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

936 Views Asked by At

I'm using the google-api-python-client library to upload apks to Google Playstore. All worked well until today. I did not modify my upload script but now i am getting the error:

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."**

This is my upload script:

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'])

I am using google-api-python-client 1.7.11.

I could not find anything about the error online. Has anyone here an idea?

Update

Thanks to the reply from @Silvio i could get my script to work again by adding the track to the request body as well:

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'])
4

There are 4 best solutions below

1
On BEST ANSWER

Add "track" to the request body as well.

e.g.

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

This worked for me. You can check the expected request body structure here:

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

0
On

I'm having similar problems publishing with Codemagic Google Play publishing fails with "Track names in request path and request body must match", looks like something might be wrong on Google's side

0
On

I am also having similar issue, opened issue here.

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

If all the folks can tag on this issue that would help.

I find myself in similar situation, it worked last week and now its broken...

0
On

For other people who use java version of Android publisher, i had the same error.

maven artifact : google-api-services-androidpublisher

java error during publish :

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"
}

To solve it, we must set the track attribute of the "com.google.api.services.androidpublisher.model.Track" object

The code will be like this :

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

In my case, it was missing this :

.setTrack(trackValue);