Google api 即使在服务器到服务器身份验证之后,也无法使用Google Play Developer API

Google api 即使在服务器到服务器身份验证之后,也无法使用Google Play Developer API,google-api,google-api-client,google-api-nodejs-client,play-billing-library,Google Api,Google Api Client,Google Api Nodejs Client,Play Billing Library,我正试图从本地服务器呼叫,但收到一条错误消息,上面写着error:Login Required 我已经创建了一个具有项目所有者角色的服务帐户,如上所述。然后我设置了GOOGLE\u APPLICATION\u CREDENTIALS环境变量,并将其指向前面提到的下载的JSON文件 最后,我尝试运行一些示例代码,用于基于的服务器到服务器身份验证,以查看是否可以成功进行身份验证: import{google}来自'googleapis' 异步函数main(){ 试一试{ const auth=new

我正试图从本地服务器呼叫,但收到一条错误消息,上面写着
error:Login Required

我已经创建了一个具有项目所有者角色的服务帐户,如上所述。然后我设置了
GOOGLE\u APPLICATION\u CREDENTIALS
环境变量,并将其指向前面提到的下载的JSON文件

最后,我尝试运行一些示例代码,用于基于的服务器到服务器身份验证,以查看是否可以成功进行身份验证:

import{google}来自'googleapis'
异步函数main(){
试一试{
const auth=new google.auth.GoogleAuth({
范围:['https://www.googleapis.com/auth/androidpublisher']
})
const authClient=wait auth.getClient()
const project=await auth.getProjectId()
const publisher=google.androidpublisher('v3')
const result=wait publisher.purchases.subscriptions.get({
packageName:'com.mywebsite.subdomain',
subscriptionId:'com.mywebsite.subscription\u name',
令牌:“…我的购买令牌…”
})
console.log(结果)
}捕获(错误){
控制台错误(错误)
}
}
main()
我只是使用了计费API而不是计算API,否则我的示例与中给出的示例相同。我不知道为什么我有问题,任何帮助将不胜感激


完全错误:

{ Error: Login Required
    at Gaxios.request (/Users/squadri/Desktop/googlenode/node_modules/gaxios/src/gaxios.ts:86:15)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  response:
   { config:
      { url:
         'https://www.googleapis.com/androidpublisher/v3/applications/com.mywebsite.subdomain/purchases/subscriptions/com.mywebsite.subscription_name/tokens/...my%20purchase%20token...',
        method: 'GET',
        paramsSerializer: [Function],
        headers: [Object],
        params: [Object: null prototype] {},
        validateStatus: [Function],
        retry: true,
        responseType: 'json',
        retryConfig: [Object] },
     data: { error: [Object] },
     headers:
      { 'alt-svc': 'quic=":443"; ma=2592000; v="46,43,39"',
        'cache-control': 'private, max-age=0',
        connection: 'close',
        'content-encoding': 'gzip',
        'content-type': 'application/json; charset=UTF-8',
        date: 'Tue, 20 Aug 2019 04:41:29 GMT',
        expires: 'Tue, 20 Aug 2019 04:41:29 GMT',
        server: 'GSE',
        'transfer-encoding': 'chunked',
        vary: 'Origin, X-Origin',
        'www-authenticate': 'Bearer realm="https://accounts.google.com/"',
        'x-content-type-options': 'nosniff',
        'x-frame-options': 'SAMEORIGIN',
        'x-xss-protection': '1; mode=block' },
     status: 401,
     statusText: 'Unauthorized' },
  config:
   { url:
      'https://www.googleapis.com/androidpublisher/v3/applications/com.mywebsite.subdomain/purchases/subscriptions/com.mywebsite.subscription_name/tokens/...my%20purchase%20token...',
     method: 'GET',
     paramsSerializer: [Function],
     headers:
      { 'x-goog-api-client': 'gdcl/3.1.0 gl-node/10.16.1 auth/5.2.0',
        'Accept-Encoding': 'gzip',
        'User-Agent': 'google-api-nodejs-client/3.1.0 (gzip)',
        Accept: 'application/json' },
     params: [Object: null prototype] {},
     validateStatus: [Function],
     retry: true,
     responseType: 'json',
     retryConfig:
      { currentRetryAttempt: 0,
        retry: 3,
        retryDelay: 100,
        httpMethodsToRetry: [Array],
        noResponseRetries: 2,
        statusCodesToRetry: [Array] } },
  code: 401,
  errors:
   [ { domain: 'global',
       reason: 'required',
       message: 'Login Required',
       locationType: 'header',
       location: 'Authorization' } ] }

您需要将JSON密钥文件添加到
google.auth.GoogleAuth

const auth = new google.auth.GoogleAuth({
  keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
  scopes: ['https://www.googleapis.com/auth/androidpublisher']
});

请参阅:

例如,尝试此代码段。
使用googleapi、androidpublisher和服务帐户身份验证打印特定packageName的apks列表(v3)

const{google}=require('googleapis');
const key=require('./privateKey.json')
const packageName=“com.company.example”
让client=new google.auth.JWT(
key.client_电子邮件,
未定义,
密钥,私钥,
['https://www.googleapis.com/auth/androidpublisher']
)
const androidApi=google.androidpublisher({
版本:“v3”,
认证:客户端
})
异步函数getApksList(){
let authorize=等待client.authorize();
//插入编辑
log('authorize:',authorize);
let res=wait androidApi.edits.insert({
packageName:packageName
})
//获取编辑id
让editId=res.data.id
const res=wait androidApi.edits.apks.list({
editId:editId,
packageName:packageName
});
log(`Result${(JSON.stringify(res))}`);
}

getApksList().catch(console.error)您需要将auth字段(authClient)传递给google.androidpublisher

const publisher = google.androidpublisher({
  version: 'v3',
  auth: authClient
})

Saad你解决了这个问题吗?嗨,Randima,对不起,我不记得我是如何解决这个问题的。当它发生时,我确实在这里打开了一个GitHub问题:似乎有一些您可能想要查看的回复。嗨Saad,谢谢您的回复。我为我的服务帐户启用了Google Play Android开发者API,并使用GoogleAuth附加了服务帐户密钥文件。这解决了我的问题。