我如何使用 Python 脚本访问 Google Playstore API 以获取对我们应用程序的评论?
How do I access Google Playstore API to get reviews of our app using Python script?
我有一个服务帐户和为该应用程序帐户生成的私钥。
testxyz.json - 包含私钥和服务帐户信息。
这是脚本:
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
credentials = ServiceAccountCredentials.from_json_keyfile_name('testxyz.json',scopes=['https://www.googleapis.com/auth/androidpublisher'])
http = httplib2.Http()
http = credentials.authorize(http)
service = build('androidpublisher', 'v3', http=http)
print(service)
package_name = "xtestx"
reviews_resource = service.reviews()
print(reviews_resource)
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
reviews_list = reviews_page["reviews"]
infinite_loop_canary = 100
while "tokenPagination" in reviews_page:
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
token=reviews_page["tokenPagination"]["nextPageToken"],
maxResults=100).execute()
reviews_list.extend(reviews_page["reviews"])
infinite_loop_canary -= 1
if infinite_loop_canary < 0:
break
脚本中的行 - reviews_resource.list(packageName=package_name,maxResults=100).execute()- 抛出“调用者没有权限”
我不确定需要做什么。感谢任何帮助。
我把答案贴在这里是因为我花了很长时间才弄明白这个问题,也许它会对其他人有所帮助。
之前你需要什么:
- 的服务帐户
SA 的私钥。 (使用 .p12 文件)
在 Play 管理中心,我们需要从开发者帐户授予对此 SA 的访问权限 -> API 访问权限。
单击“授予访问权限”(从第 3 步开始)后,我们会看到“权限”对话框 - 为所需应用启用“回复评论”。
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client.service_account import ServiceAccountCredentials
SERVICE_ACCOUNT_EMAIL = ('associated_service_email_address_goes_here')
#this is the private key file
key = 'xxx.p12'
scope='https://www.googleapis.com/auth/androidpublisher'
credentials= ServiceAccountCredentials.from_p12_keyfile(SERVICE_ACCOUNT_EMAIL,
key,
scopes=[scope])
package_name = "xtestx"
try:
http = httplib2.Http()
http = credentials.authorize(http)
service = build('androidpublisher', 'v3', http=http)
reviews_resource = service.reviews()
print(reviews_resource)
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
reviews_list = reviews_page["reviews"]
except Exception as e:
...
权限造成了我的问题。
我有一个服务帐户和为该应用程序帐户生成的私钥。
testxyz.json - 包含私钥和服务帐户信息。
这是脚本:
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
credentials = ServiceAccountCredentials.from_json_keyfile_name('testxyz.json',scopes=['https://www.googleapis.com/auth/androidpublisher'])
http = httplib2.Http()
http = credentials.authorize(http)
service = build('androidpublisher', 'v3', http=http)
print(service)
package_name = "xtestx"
reviews_resource = service.reviews()
print(reviews_resource)
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
reviews_list = reviews_page["reviews"]
infinite_loop_canary = 100
while "tokenPagination" in reviews_page:
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
token=reviews_page["tokenPagination"]["nextPageToken"],
maxResults=100).execute()
reviews_list.extend(reviews_page["reviews"])
infinite_loop_canary -= 1
if infinite_loop_canary < 0:
break
脚本中的行 - reviews_resource.list(packageName=package_name,maxResults=100).execute()- 抛出“调用者没有权限”
我不确定需要做什么。感谢任何帮助。
我把答案贴在这里是因为我花了很长时间才弄明白这个问题,也许它会对其他人有所帮助。
之前你需要什么:
- 的服务帐户
SA 的私钥。 (使用 .p12 文件)
在 Play 管理中心,我们需要从开发者帐户授予对此 SA 的访问权限 -> API 访问权限。
单击“授予访问权限”(从第 3 步开始)后,我们会看到“权限”对话框 - 为所需应用启用“回复评论”。
from apiclient.discovery import build import httplib2 from oauth2client import client from oauth2client.service_account import ServiceAccountCredentials SERVICE_ACCOUNT_EMAIL = ('associated_service_email_address_goes_here') #this is the private key file key = 'xxx.p12' scope='https://www.googleapis.com/auth/androidpublisher' credentials= ServiceAccountCredentials.from_p12_keyfile(SERVICE_ACCOUNT_EMAIL, key, scopes=[scope]) package_name = "xtestx" try: http = httplib2.Http() http = credentials.authorize(http) service = build('androidpublisher', 'v3', http=http) reviews_resource = service.reviews() print(reviews_resource) reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute() reviews_list = reviews_page["reviews"] except Exception as e: ...
权限造成了我的问题。