javascriptnode.jsgoogle-apiapkgoogle-publisher-tag

Can't Upload APK to Google Play Developer via Publisher API


i'm creating script using nodejs to upload my APK to Google Play Developer via Publishing API, however it failed. I think, it's nothing wrong with APK file, because it's good file. so any idea to solve this ?

i also try multipart upload, but return error ( i will attach here soon )

Below are the Error message :

Upload successful!  Server responded with: {
 "error": {
  "errors": [
   {
    "domain": "androidpublisher",
    "reason": "apkInvalidFile",
    "message": "Invalid APK file."
   }
  ],
  "code": 403,
  "message": "Invalid APK file."
 }
}

My Source Code

var https = require('https');
var request = require('request');
https.post = require('https-post');
var fs = require('fs');

var tokens = process.argv[2];

var formData = {
    my_file: fs.createReadStream('example_file.apk')
}   



if (tokens != '')
{
    var url_create_listing = 'https://www.googleapis.com/androidpublisher/v2/applications/com.example.apps/edits?access_token='+tokens;
    request.post( url_create_listing, function (error, response, body) {
        var str_body = JSON.parse (body);
        var listing_id = str_body.id;

        var url_upload = 'https://www.googleapis.com/upload/androidpublisher/v2/applications/com.example.apps/edits/'+listing_id+'/apks?uploadType=media&access_token='+tokens;


        var options = {
          url: url_upload,
          headers: {
            'Content-Type': 'application/vnd.android.package-archive'
          }
        };

        request.post( options, function optionalCallback(err, httpResponse, body)
         {
          if (err) {
            return console.error('upload failed:', err);
          }
          console.log('Upload successful!  Server responded with:', body);
        }).pipe(fs.createReadStream('example_file.apk'));
    }); 
}

Solution

  • I think you have a misunderstanding of how NodeJS streams works.

    In the sample code you have shown above you are trying to pipe the result of request.post into fs.createReadStream which doesn't make sense and that's reason for the error as you are not sending any apk file. Instead it should be done like the following. (I am not sure how androidpublisher api works, I assume your sample shows the correct usage.)

    var url_create_listing = 'https://www.googleapis.com/androidpublisher/v2/applications/com.example.apps/edits?access_token='+tokens;
    
    request.post( url_create_listing, function (error, response, body) {
    var str_body = JSON.parse (body);
    var listing_id = str_body.id;
    var url_upload = 'https://www.googleapis.com/upload/androidpublisher/v2/applications/com.example.apps/edits/'+listing_id+'/apks?uploadType=media&access_token='+tokens;
    
    // request module is capable of auto detecting the Content-Type header
    // based on the file extension.
    var options = {
      url: url_upload
    };
    
    fs.createReadStream('example_file.apk')
    .pipe(request.post(options, function optionalCallback(err, httpResponse, body){
      if (err) {
        return console.error('upload failed:', err);
      }
      console.log('Upload successful!  Server responded with:', body);
    }));