首页 > 解决方案 > Google Developer API 使用 Python 上传 APK

问题描述

我正在尝试使用 Developer API 将 APK 上传到 Google Play。我有一个主要工作的 Python 脚本,但上传时不断返回“请求‘APK 大小太大’时的 HttpError 403”错误。我们通过网络界面手动上传和发布应用程序没有问题。我在某处缺少设置吗?

这是我当前的脚本:

import argparse
from googleapiclient.discovery import build
import google_auth_httplib2
import httplib2
from oauth2client import client
from google.oauth2 import service_account
from google.auth.transport.requests import Request
import sys, os, socket


def main():
    pathname = os.path.dirname(sys.argv[0])    
    fullpath = os.path.abspath(pathname)
    
    SCOPES = ['https://www.googleapis.com/auth/androidpublisher']
    SERVICE_ACCOUNT_FILE = fullpath + '/authorization_data.json' # access keys json file
    
    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    credentials.refresh(Request())
    print(credentials.token)

    http = httplib2.Http(timeout=300)
    http = google_auth_httplib2.AuthorizedHttp(credentials, http=http)
    
    service = build('androidpublisher', 'v3', http=http)
    
    package_name = 'com.company.app'
    apk_file = 'upload/myapp.apk'
    
    try:
        print('creating edit')
        edit_request = service.edits().insert(body={}, packageName=package_name)
        result = edit_request.execute()
        edit_id = result['id']
        print('id: ' + edit_id)
        
        print('uploading apk')
        apk_response = service.edits().apks().upload(
            editId=edit_id,
            packageName=package_name,
            media_body=apk_file
            ).execute()
        print('Version code %d has been uploaded' % apk_response['versionCode'])

        print('committing edit')
        commit_request = service.edits().commit(
            editId=edit_id, 
            packageName=package_name
            ).execute()
        
        print('Edit "%s" has been committed' % commit_request['id'])

    except client.AccessTokenRefreshError:
        print('The credentials have been revoked or expired, please re-run the application to re-authorize')


if __name__ == '__main__':
    main()

标签: pythonandroidgoogle-play-developer-api

解决方案


推荐阅读