【问题标题】:Google service account DelegationGoogle 服务帐号委派
【发布时间】:2017-03-06 23:33:52
【问题描述】:

我正在努力在 google appengine 中创建一个简单的应用程序,以自动在我的 blogspot 上发布内容。

我已经创建了服务帐户并尝试使用以下代码来调用博客 api 来发布博客。这个运行失败,权限不足错误消息。

urlfetch.set_default_fetch_deadline(45)
service = build('blogger', 'v3')

class MainHandler(webapp2.RequestHandler):
    def post(self):
            body = {
            "kind": "blogger#post",
            "id": "9999999999999999",
            "title": "SAmple title",
            "content": "SAmple blog contents"
            }
            scopes = ['https://www.googleapis.com/auth/blogger']
            credentials = ServiceAccountCredentials.from_json_keyfile_name('secret-key.json', scopes=scopes)
            http_auth     = credentials.authorize(Http())
            request = service.posts().insert(blogId="9999999999999999",body=body)
            response = request.execute(http=http_auth)
            self.response.out.write(pprint.pformat(response))

app = webapp2.WSGIApplication([
    ('/postcontents', MainHandler)
], debug=True)

但是我可以通过以下行访问博客详细信息

self.response.write(blogs.get(blogId='9999999999999999').execute(http=http_auth))

我的服务帐户似乎与我的博主帐户没有关联(尽管我的谷歌应用引擎和博主都使用相同的 gmail 帐户)。我如何做到这一点?

【问题讨论】:

    标签: python google-app-engine blogger google-api-python-client service-accounts


    【解决方案1】:

    Blogger 不支持服务帐户。您将需要使用 Oauth2。存储刷新令牌后验证您的代码拥有您的代码,然后根据需要使用刷新令牌获取新的访问令牌。

    【讨论】:

    • 感谢您的意见。最初,我尝试使用 OAuth 2.0 令牌,验证了我的代码并获得了刷新令牌。我能够访问博客 API。但是一段时间后,如果我使用相同的令牌访问 APi,它会指示我登录谷歌帐户以生成新令牌。我如何再次使用相同的令牌来获取新的访问令牌?我打算为博客文章安排一个 cron 作业。任何文档或示例代码都会对我有所帮助。
    • python 客户端库应该处理获取新的访问令牌。 Refreshtoken 可以过期,您只需要重新授权即可。注意:这是您拥有使用 oauth2 的工作代码的唯一选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2021-12-11
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多