如何使用api-3和OAuth-2.0修复401更新博客的未经授权的错误

问题描述 投票:1回答:1

我正在尝试使用javascript代码更新使用博客制作的博客中的帖子,我已完成所有操作,因为文档说使用OAuth2但我仍然得到响应“401 Unauthorized”我不知道这个代码有什么问题?注意:我已经创建了signIn代码并且它正在工作..

var user = GoogleAuth.currentUser.get();
if (user.Zi!=null){TheToken="Bearer " + user.Zi.access_token;} 

$.ajax
      ({
        url: 'https://www.googleapis.com/blogger/v3/blogs/355327149591714411/posts/7690459698185134878',
        type: 'put',
        Authorization: TheToken,
        contentType: 'application/json',
        data:
        {
          'kind': 'blogger#post',
          'id': '7690459698185134878',
          'blog': {'id': '355327149591714411'},
          'url': 'https://www.wmccoregon.org/2019/04/data-of-settings.html',
          'selfLink': 'https://www.googleapis.com/blogger/v3/blogs/355327149591714411/posts/7690459698185134878',
          'title': 'Data of Settings',
          'content': DataToUpdate
        },
        success: function(data)
        {
          AdminData(vReadSettings,0,0);
        },
        error: function(xhr)
        {
          alert("An error occured: " + xhr.status + " " + xhr.statusText + " " + xhr.responseText);
        }
      });
javascript oauth-2.0 blogger
1个回答
0
投票

你确定你实际上正确地在标题中传递了持票人令牌吗?

据我所知,Authorization不是jquery.ajax的选项(除非你将它扩展到某个地方)。如果您使用的是承载令牌,则您创建的字符串看起来是正确的,但您需要在Authorization标头中传递它。

就像是:

$.ajax
  ({
    url: 'https://www.googleapis.com/blogger/v3/blogs/355327149591714411/posts/7690459698185134878',
    type: 'put',
    headers: {
      "Authorization": TheToken
    },
  });
© www.soinside.com 2019 - 2024. All rights reserved.