我正在使用谷歌python libraty为我的应用程序请求ANR数据,脚本非常简单
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret.
CLIENT_SECRETS_FILE = "client_secret.json"
# This access scope grants read-only access to the authenticated user's apps
SCOPES = ['https://www.googleapis.com/auth/androidpublisher']
API_SERVICE_NAME = 'playdeveloperreporting'
API_VERSION = 'v1beta1'
ANR_METHOD_NAME = 'apps/com.my.app/anrRateMetricSet'
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRETS_FILE,
scopes=SCOPES)
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_local_server(host='localhost',
port=5000,
authorization_prompt_message='Please visit this URL: {url}',
success_message='The auth flow is complete; you may close this window.',
open_browser=True)
return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
def getAnrReport():
report_service = get_authenticated_service()
vitals = report_service.vitals()
anr = vitals.anrrate()
userPerceivedAnrRate7dUserWeighted = anr.get(name=ANR_METHOD_NAME, x__xgafv='2').execute()
report_service.close()
# ...
if __name__ == '__main__':
getAnrReport()
但由于某种原因,我得到了一个错误
googleapiclient.errors.HttpError:
HttpError 403 when requesting https://playdeveloperreporting.googleapis.com/v1beta1/apps/com.my.app/anrRateMetricSet?%24.xgafv=2&alt=json
returned "Request had insufficient authentication scopes.".
Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'method': 'google.play.developer.reporting.v1beta1.VitalsService.GetAnrRateMetricSet', 'service': 'playdeveloperreporting.googleapis.com'}}]"
文档https://developers.google.com/play/developer/reporting/reference/rest/v1beta1/vitals.anrrate/get显示
授权范围需要以下OAuth范围:
https://www.googleapis.com/auth/playdeveloperreporting
更高级别的文档https://developers.google.com/play/developer/reporting/reference/rest/v1beta1/vitals.anrrate指出
所需权限:要访问此资源,调用用户需要对应用程序具有查看应用程序信息(只读)权限。
我确实拥有该权限,我的Google用户可以在Google Play控制台中访问应用信息和ANR费率。我还根据文档要求playdeveloperreporting
范围,但由于某种原因范围不足,我做错了什么?
注意:脚本中的作用域是https://www.googleapis.com/auth/androidpublisher
文档指出它应该是https://www.googleapis.com/auth/playdeveloperreporting,但由于以下错误,无法请求此范围:
Some requested scopes cannot be shown: [https://www.googleapis.com/auth/playdeveloperreporting]
Error 400: invalid_scope
1条答案
按热度按时间3bygqnnd1#
在python项目https://github.com/googleapis/google-api-python-client/issues/1761的问题中找到了答案
API目前仅支持GCP服务帐户的访问
因此,要使用该API,我需要一个服务帐户,而不是一个需要通过Web授权的人类帐户。
服务帐户的使用方法大致如下:
服务帐户的使用方法大致如下:
1.创建GCP使用者项目并为其获取API密钥
1.将您的GCP消费者项目链接到API访问UI中的Play Console开发者帐户
1.创建服务帐户(https://cloud.google.com/iam/docs/service-accounts),并下载帐户的密钥文件(相当于其密码)
1.“邀请”该服务帐户成为您的Play Console开发人员的用户,并至少授予部分或所有应用的“只读”访问权限。
1.使用诸如oauth2l之类的工具交换OAuth令牌的服务帐户密钥。