Android 发布者:"Track names in request path and request body must match."

Android Publisher: "Track names in request path and request body must match."

我正在使用 google-api-python-客户端库将 apk 上传到 Google Playstore。直到今天,一切都运作良好。我没有修改上传脚本,但现在出现错误:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/androidpublisher/v3/applications/[packageName]/edits/[editId]/tracks/internal?alt=json returned 
**"Track names in request path and request body must match."**

这是我的上传脚本:

from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client.service_account import ServiceAccountCredentials
import json
import mimetypes

package_name = '...'
keyfile_content = '...'
apk_file = '...'
release_name = '...'
track = 'internal'

mimetypes.add_type("application/octet-stream", ".apk")
mimetypes.add_type("application/octet-stream", ".aab")

keyfile_dict = json.loads(keyfile_content)
credentials = ServiceAccountCredentials.from_json_keyfile_dict(keyfile_dict, 'https://www.googleapis.com/auth/androidpublisher')
http = httplib2.Http()
http = credentials.authorize(http)

service = build('androidpublisher', 'v3', http=http)

edit_request = service.edits().insert(body={}, packageName=package_name)
result = edit_request.execute()
edit_id = result['id']

apk_response = service.edits().apks().upload(
        editId=edit_id,
        packageName=package_name,
        media_body=apk_file).execute()

print('Version code %d has been uploaded but not yet committed' % apk_response['versionCode'])

if track:
    track_response = service.edits().tracks().update(
        editId=edit_id,
        track=track,
        packageName=package_name,
        body={u'releases': [{
            u'name': release_name,
            u'versionCodes': [str(apk_response['versionCode'])],
            u'status': u'completed',
        }]}).execute()

    print('Track %s is set with releases: %s' % (
        track_response['track'], str(track_response['releases'])))
else:
    print('No track specified')

commit_request = service.edits().commit(
        editId=edit_id, packageName=package_name).execute()

print('Edit "%s" has been committed' % commit_request['id'])

我正在使用 google-api-python-client 1.7.11.

我在网上找不到任何关于错误的信息。这里有人有想法吗?

更新

感谢来自@Silvio 的 我可以通过将轨道添加到请求正文来让我的脚本再次工作:

from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client.service_account import ServiceAccountCredentials
import json
import mimetypes

package_name = '...'
keyfile_content = '...'
apk_file = '...'
release_name = '...'
track = 'internal'

mimetypes.add_type("application/octet-stream", ".apk")
mimetypes.add_type("application/octet-stream", ".aab")

keyfile_dict = json.loads(keyfile_content)
credentials = ServiceAccountCredentials.from_json_keyfile_dict(keyfile_dict, 'https://www.googleapis.com/auth/androidpublisher')
http = httplib2.Http()
http = credentials.authorize(http)

service = build('androidpublisher', 'v3', http=http)

edit_request = service.edits().insert(body={}, packageName=package_name)
result = edit_request.execute()
edit_id = result['id']

apk_response = service.edits().apks().upload(
        editId=edit_id,
        packageName=package_name,
        media_body=apk_file).execute()

print('Version code %d has been uploaded but not yet committed' % apk_response['versionCode'])

if track:
    track_response = service.edits().tracks().update(
        editId=edit_id,
        track=track,
        packageName=package_name,
        body={u'track': track,
            u'releases': [{
            u'name': release_name,
            u'versionCodes': [str(apk_response['versionCode'])],
            u'status': u'completed',
        }]}).execute()

    print('Track %s is set with releases: %s' % (
        track_response['track'], str(track_response['releases'])))
else:
    print('No track specified')

commit_request = service.edits().commit(
        editId=edit_id, packageName=package_name).execute()

print('Edit "%s" has been committed' % commit_request['id'])

我在使用 Codemagic 发布时遇到了类似的问题,看起来 Google 方面可能有问题

也将 "track" 添加到请求正文中。

例如

body={u'track': track,
u'releases': [{
u'name': release_name,
u'versionCodes': [str(apk_response['versionCode'])],
u'status': u'completed',
...

这对我有用。您可以在此处检查预期的请求主体结构:

https://developers.google.com/android-publisher/api-ref/edits/tracks#resource

我也有类似的问题,在这里打开问题。

https://github.com/googleapis/google-api-python-client/issues/840

如果所有人都可以在这个问题上添加标签,那将会有所帮助。

我发现自己处于类似情况,它上周工作,现在坏了...

对于使用 java 版 Android 发布者的其他人,我遇到了同样的错误。

maven 神器:google-api-services-androidpublisher

java 发布时出错:

Exception in thread "main" com.netfinca.mobile.publish.apk.PublisherHelperException: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Track names in request path and request body must match.",
    "reason" : "badRequest"
  } ],
  "message" : "Track names in request path and request body must match.",
  "status" : "INVALID_ARGUMENT"
}

要解决这个问题,我们必须设置"com.google.api.services.androidpublisher.model.Track"对象的track属性

代码将是这样的:

Edit edit = new AndroidPublisher.Builder(...);
Track trackObject = new Track().setTrack(trackValue); // trackValue could be internal, alpha, ...
edit.tracks().update(androidPackage, editId, trackValue, trackObject );

就我而言,它缺少这个:

.setTrack(trackValue);