【问题标题】:Trying to delete a post using Blogger API returns "not found" error尝试使用 Blogger API 删除帖子会返回“未找到”错误
【发布时间】:2012-08-07 01:51:28
【问题描述】:

向 Blogger REST API (v3.0) 发送 DELETE 请求,我正在尝试使用 delete method 删除帖子。为此,我使用以下代码:

api_uri = 'https://www.googleapis.com/blogger/v3/blogs/%s/posts/%s' % (blogId, postId)
result = urlfetch.fetch(url=api_uri,
                  method=urlfetch.DELETE,
                  headers={'Authorization' : oauth_token})

self.response.out.write(result.content)

但是服务器返回:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

但是,我可以使用以下代码检索有关此帖子的信息:

api_uri = 'https://www.googleapis.com/blogger/v3/blogs/%s/posts/%s' % (blogId, postId)
result = urlfetch.fetch(url=api_uri,
                  headers={'Authorization' : oauth_token})
self.response.out.write(result.content)

此时,我无法理解我做错了什么——请求被授权,blogIdpostId 是正确的——但无论如何,服务器返回“未找到”错误。

如果你知道如何解决这个问题或者你可以提供有用的建议——请帮助我。
感谢您抽出宝贵时间考虑此事。


UPD 1:如果我向以下 URL 发送请求:

# https://www.googleapis.com/blogger/v3/users/{userID}
# https://www.googleapis.com/blogger/v3/users/self

服务器也返回:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}


UPD 2:我忘了说我使用的是OAuth 2.0 for Server to Server Applications。因此,为了获取授权令牌,我使用以下 JWT 声明集向https://accounts.google.com/o/oauth2/token 发送请求:

jwt_claim_set = {
   'iss' : '{id}@developer.gserviceaccount.com',
   'scope' : 'https://www.googleapis.com/auth/blogger',
   'aud' : 'https://accounts.google.com/o/oauth2/token',
   'exp' : expire,
   'iat' : timestamp
}

服务器返回:

{
  "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

并定义变量oauth_token,使用:

data = simplejson.loads(result.content)
oauth_token = data['token_type'] + ' ' + data['access_token']

【问题讨论】:

  • 考虑到您可能已经检查了 ID 和身份验证令牌,我建议您使用 cURL 手动测试您的查询。根据结果​​,您将知道问题是您如何构建 url,还是其他什么...
  • @AlexisHuet,感谢您的建议,但无论如何,我确定问题不在 URL 中。当然,问题出在“别的东西”上。
  • 你用requests试过了吗,看看是不是你使用的模块有问题?
  • 只是删除失败了吗?或者您也有类似的编辑问题?

标签: python api blogger


【解决方案1】:

您确定您正确使用了 OAuth2 吗?在我看来,您没有正确登录,这就是您收到这些错误的原因。

使用 Google OAuh2 Playground (https://code.google.com/oauthplayground/) 尝试同样的查询,看看会发生什么。

【讨论】:

  • 感谢您的留言,但正如我所说,我正在尝试将 OAuth 2.0 用于服务器到服务器应用程序。目前 OAuth 2.0 Playground 不支持这种授权方式。至于正确使用 OAuth2,我认为,如果出现问题,就会显示另一个错误。
  • 你为什么不尝试在标题中添加授权而不是在 URL 中添加 ?access_token= ?反正我就是这样做的。
  • 根据documentation,需要发送Authorization header。您的意思是您将 access_token=<token> 与 OAuth 2.0 一起用于服务器到服务器应用程序?
猜你喜欢
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多