【问题标题】:Can't Upload APK to Google Play Developer via Publisher API无法通过 Publisher API 将 APK 上传到 Google Play Developer
【发布时间】:2018-01-16 04:16:18
【问题描述】:

我正在使用 nodejs 创建脚本以通过 Publishing API 将我的 APK 上传到 Google Play Developer,但是它失败了。我认为,APK文件没什么问题,因为它是一个好文件。那么有什么办法解决这个问题吗?

我也尝试分段上传,但返回错误(我将很快附上)

以下是错误消息:

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

我的源代码

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'));
    }); 
}

【问题讨论】:

    标签: javascript node.js google-api apk google-publisher-tag


    【解决方案1】:

    我认为您对 NodeJS 流的工作方式存在误解。

    在上面显示的示例代码中,您试图将request.post 的结果通过管道传输到fs.createReadStream,这没有意义,这就是错误的原因,因为您没有发送任何apk 文件。相反,它应该像下面那样完成。 (我不确定 androidpublisher api 是如何工作的,我假设您的示例显示了正确的用法。)

    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);
    }));
    

    【讨论】:

      【解决方案2】:

      根据https://developers.google.com/games/services/publishing/:

      Google Play 游戏服务发布 API 可让您自动化 经常要做的任务(...)

      您是否受到绝对限制使用 JS 和 node.js 来完成这项任务?如果没有,您可能会发现这个 Google management-tools 代码示例很有用。

      在此存储库中,您可以找到帮助您发布、管理和 测试您的 Play 游戏服务应用

      在我看来,最好还是安装 Python(并重用官方代码)来做到这一点。

      (Google 开发人员显然更喜欢 python 来完成这种任务)

      【讨论】:

        【解决方案3】:

        你看过fastlane.tools吗?发布、上传、截屏、松弛通知等的完整脚本,适用于 Android 和 iOS。

        【讨论】:

          猜你喜欢
          • 2014-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-23
          • 1970-01-01
          • 2016-06-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多