用于具有OAuth2服务帐户的Chrome Store Publishing API



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

我需要使用服务器到服务器的身份验证方法,因此在谷歌开发者控制台上为我的项目创建了一个oauth2服务帐户。并下载了凭据作为密钥.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 = (
'_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是否发生,因为服务帐户没有访问我的谷歌chrome网店项目的权限
  2. 是否可以在不使用我的个人帐户的情况下验证和使用Chrome Store API,该帐户发布到网络商店(但连接的服务帐户)
  3. 在没有用户通过flow进行身份验证的情况下,我如何检索用于Chrome Web Store API的有效authToken

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

  1. 用户

    a) 为了让服务帐户代表向chrome web商店发布项目的用户,您需要在创建凭据时添加一个子参数:

    凭据=客户端。签名JwtAssertionCredentials(SERVICE_ACCOUNT_MAIL,键,scope='https://www.googleapis.com/auth/chromewebstore.readonly',sub='the.user.who.published.the.item@gmail.com')

    b)如果用户是Google应用程序域的一部分,则Chrome Web Store Publish API需要在Google应用程序仪表板>安全>扩展设置>管理API访问中授予域范围用户访问权限

  2. 获取项的API调用具有所需的查询参数projection,其值为draftpublished。文档中说它是可选的,但实际上它是必需的。

通过这两项更改,API可以与http对象一起使用。感谢你们所有人,他们帮助我找到了解决方案,感谢谷歌的支持,他们指出了所需的参数。

  1. 是否发生403,因为服务帐户没有访问我的谷歌chrome网店项目的权限

可能。服务帐户是而不是您自己的谷歌帐户。

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

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

  1. 在没有用户通过流进行身份验证的情况下,如何检索用于Chrome Web Store API的有效authToken

通过"离线访问"授权一次,将返回一个刷新令牌。您可以随时使用它,即使没有登录,也可以请求访问令牌。顺便说一句,不存在"authtoken"这样的东西。有一个授权码和一个访问令牌。在您的情况下,它是您感兴趣的访问令牌。

最新更新