11

问题:我一直在使用Google 的 Python 脚本示例将 apk 上传到 Play 商店并获取通过我的帐户(list_apks.pyupload_apk.py)发布的应用程序列表。然而最近它开始破裂。我试图通过这样做来更新诸如等之类的软件包google-api-python-client,但没有帮助。oath2clientpip install --update packagename

日志

如果在列出 apk 时:

Determining latest version for my.package.name...
error   25-Feb-2016 06:30:52    Traceback (most recent call last):
error   25-Feb-2016 06:30:52      File "list_apks.py", line 80, in <module>
error   25-Feb-2016 06:30:52        main()
error   25-Feb-2016 06:30:52      File "list_apks.py", line 46, in main
error   25-Feb-2016 06:30:52        credentials = client.SignedJwtAssertionCredentials(
error   25-Feb-2016 06:30:52    AttributeError: 'module' object has no attribute 'SignedJwtAssertionCredentials'
build   25-Feb-2016 06:30:52    Found latest APK version: 
build   25-Feb-2016 06:30:52    Generated new APK version: 1

这是上传apk时:

25-Feb-2016 06:33:30    Uploading APK...
25-Feb-2016 06:33:30    Traceback (most recent call last):
25-Feb-2016 06:33:30      File "upload_apk.py", line 115, in <module>
25-Feb-2016 06:33:30        main(sys.argv)
25-Feb-2016 06:33:30      File "upload_apk.py", line 62, in main
25-Feb-2016 06:33:30        credentials = client.SignedJwtAssertionCredentials(
25-Feb-2016 06:33:30    AttributeError: 'module' object has no attribute 'SignedJwtAssertionCredentials'

代码片段

import argparse

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


SERVICE_ACCOUNT_EMAIL = (
    'myaccountemail.com')

# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('package_name',
                       help='The package name. Example: com.android.sample')


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

  # HERE IS THE EXCEPTION 
  credentials = client.SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      key,
      scope='https://www.googleapis.com/auth/androidpublisher')
  http = httplib2.Http()
  http = credentials.authorize(http)
  ...

我还能尝试什么?我会很感激你的帮助。

4

2 回答 2

30

经过这么多天,我终于找到了答案。事实证明,该类SignedJwtAssertionCredentials已从更新中的oath2clientpython 包中删除2.0.0。它已经不在了oauth2client.client。该行为已移至oauth2client.service_account.ServiceAccountCredentials.

以下为我工作:

import argparse

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
from oauth2client import client

SERVICE_ACCOUNT_EMAIL = ('myaccountemail.com')

# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('package_name',
                   help='The package name. Example: com.android.sample')

def main():
  key='mykeyname.p12'
  scope = 'https://www.googleapis.com/auth/androidpublisher'

  credentials = ServiceAccountCredentials.from_p12_keyfile(
      SERVICE_ACCOUNT_EMAIL,
      key,
      scopes=[scope]
  )
  http = httplib2.Http()
  http = credentials.authorize(http)
  ....

来源:

于 2016-02-27T06:07:32.017 回答
2

我遇到了同样的问题,而且我没有安装使用from_p12_keyfile().

from_p12_keyfile()


如果未安装 pyOpenSSL / 不是活动的加密库,则引发: NotImplementedError。

我将 JSON 文件读入字典并将其传递给from_json_keyfile_dict()这样的:

import json
from oauth2client.service_account import ServiceAccountCredentials
# other imports and code from sample...

# modified authentication part
data = {}
with open('api.json') as json_file:
  data = json.load(json_file)
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
    data,
    scopes=['https://www.googleapis.com/auth/androidpublisher']
)
http = httplib2.Http()
http = credentials.authorize(http)
# do what ever you want with the returned object

api.json 文件是指创建服务帐户时创建的文件。

当然,还有其他几个功能可以获取凭据,但这个解决方案帮助我让这些东西正常工作。也许这将有助于将来的某人。

参考:Oauth2Client.service_account

于 2020-12-16T16:50:20.153 回答