python - 通过 OAuth2 服务帐户使用 Chrome Store Publishing API

标签 python google-oauth google-chrome-app chrome-web-store

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

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

然后我尝试使用 Google API Client for Python 。尽管该 API 不支持 Chrome Web Store,但应该可以使用其中的部分内容。

我用 Python 创建了一个小脚本来尝试获取我的 Chrome Web Store 项目列表:

"""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 = (
    '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adf2fee2e0e8f29c9f9b9a9e9b9c9f9b949e9cf2fff8efe4fee5f2edc9c8dbc8c1c2ddc8df83cadec8dfdbc4cec8cccecec2d8c3d983cec2c0" rel="noreferrer noopener nofollow">[email protected]</a>')

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 Store API?
  3. 如何检索有效的 authToken 以用于 Chrome Web Store API,而无需用户通过流程进行身份验证。

最佳答案

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

  1. 用户

    a) 要让服务帐户代表向 Chrome 网上应用店发布项目的用户进行操作,您需要在创建凭据时添加一个子参数:

    credentials = client.SignedJwtAssertionCredentials( SERVICE_ACCOUNT_EMAIL, key, scope='https://www.googleapis.com/auth/chromewebstore.readonly', sub='[email protected]')

    b) 如果用户属于 Google Apps 域,则需要向 Google Apps Dashboard > Security > Extended Settings > Manage API access 的域范围内的用户授予 Chrome Web Store Publish API 访问权限。

  2. API call to get an item具有必需的查询参数 projection,其值为 draftpublisheddocumentation 中将其声明为可选,但这实际上是必需的。

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

关于python - 通过 OAuth2 服务帐户使用 Chrome Store Publishing API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29567470/

相关文章:

python - 是否可以找到或匹配两个具有不同特殊字符的名称django

python - 在 python 中使用 scipy.integrate.odeint 时遇到问题

python - Pandas :基于标签位置的索引器(loc)与日期的行为

google-analytics - OAuth 2 授权码 - 它的有效期是多久?

javascript - chrome 扩展禁用单击关闭通知按钮时的通知声音

javascript - Chrome 打包应用程序 UDP 套接字无法正常工作

Python Xgboost : ValueError ('feature_names may not contain [, ] or <' )

javascript - 在 iframe(Chrome 扩展)中加载 Google 帐户登录

javascript - Chrome Extension : While Capturing Desktop, 所选窗口集中在前面

youtube-api - 将刷新 token 交换为访问 token 时出现 "invalid_grant"错误