使用 OAuth2 服务帐户 Chrome 存储发布 API

Use to Chrome Store Publishing API with OAuth2 Service Account

我尝试使用 Chrome Web Store Publishing API 以编程方式更新 chrome 应用程序。

我需要使用服务器到服务器的身份验证方法,因此在 Google developer console 上为我的项目创建了一个 oauth2 服务帐户。并将凭据下载为 key.p12.

然后我尝试使用Google API Client for Python。尽管 API 不支持 Chrome 网上商店,但应该可以使用其中的一部分。

我在 Python 中创建了一个小脚本来尝试获取我的 Chrome 网上商店商品列表:

"""Creates a connection to Google Chrome Webstore Publisher API."""

from apiclient.discovery import build
import httplib2
from oauth2client import client

import os

SERVICE_ACCOUNT_EMAIL = (
    '_SOME_126736126931_RUBISH_@developer.gserviceaccount.com')

def getservice():
    # get relative path to p12 key
  dir = os.path.dirname(__file__)
  filename = os.path.join(dir, 'key.p12')

  # Load the key in PKCS 12 format that you downloaded from the Google APIs
  # Console when you created your Service account.
  f = file(filename, 'rb')
  key = f.read()
  f.close()

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  credentials = client.SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      key,
      scope='https://www.googleapis.com/auth/chromewebstore.readonly')
  http = httplib2.Http()
  http = credentials.authorize(http)

  response = http.request('https://www.googleapis.com/chromewebstore/v1.1/items/[___my_chrome_webstore_app_id___]')

  print response

即使对 https://www.googleapis.com/auth/chromewebstore.readonly 身份验证 成功,响应结果为 403错误。

我的问题:

  1. 是否出现 403,因为服务帐户无权访问我的 google chrome 网上商店商品?
  2. 是否可以在不使用我的个人帐户的情况下验证和使用 Chrome 商店 API,发布到网上商店(但连接的服务帐户)?
  3. 如何在没有用户通过 flow.
  4. 进行身份验证的情况下检索用于 Chrome 网上商店 API 的有效 authToken
  1. 是否出现 403,因为服务帐户无权访问我的 google chrome 网上商店商品?

可能吧。服务帐户不是您自己的Google帐户。

  1. 是否可以在不使用我的个人帐户的情况下验证和使用 Chrome 商店 API,发布到网上商店(但连接的服务帐户)?

不知道。抱歉不熟悉 Chrome 商店 API。您可以使用 Oauth Playground 来测试各种可能性,而无需编写任何代码。

  1. 如何在没有用户通过流程进行身份验证的情况下检索有效的 authToken 以用于 Chrome 网上应用店 API。

通过 "offline access" 授权一次,这将 return 你一个刷新令牌。您可以随时使用它(即使未登录)来请求访问令牌。顺便说一句,没有 "authtoken" 这样的东西。有授权码和访问令牌。在您的情况下,它是您感兴趣的访问令牌。

我的脚本实际上有两个问题。

  1. 用户

    a) 要让服务帐户代表向 chrome 网上商店发布商品的用户执行操作,您需要在创建凭据时添加子参数:

    credentials = client.SignedJwtAssertionCredentials( SERVICE_ACCOUNT_EMAIL, key, scope='https://www.googleapis.com/auth/chromewebstore.readonly', sub='the.user.who.published.the.item@gmail.com')

    b) 如果用户是 Google Apps Domain 的一部分,则 Chrome Web Store Publish API 需要被授予 Google Apps Dashboard > Security > Extended Settings > Manage API access

  2. API call to get an item has a required query parameter projection with the values draft or published. It's stated as optional in the documentation,其实是必须的

通过这两项更改,API 可以与 http 对象一起使用。感谢大家帮助我找到解决方案,感谢 Google 支持,指出需要的参数。