gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:11:01 28 4
gpt4 key购买 nike

我正在尝试调用 Purchases.Subscriptions: get来 self 的本地服务器,但我收到一条错误消息,提示 Error: Login Required

我已经按照 here 所述创建了一个具有项目所有者角色的服务帐户.然后,我设置了 GOOGLE_APPLICATION_CREDENTIALS 环境变量,并使其指向已下载的 JSON 文件,如 here 所述.

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

import { google } from 'googleapis'

async function main() {
try {
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/androidpublisher']
})

const authClient = await auth.getClient()
const project = await auth.getProjectId()
const publisher = google.androidpublisher('v3')

const result = await publisher.purchases.subscriptions.get({
packageName: 'com.mywebsite.subdomain',
subscriptionId: 'com.mywebsite.subscription_name',
token: '...my purchase token...'
})

console.log(result)
} catch (error) {
console.error(error)
}
}

main()

我只是使用了 Billing API 而不是 Compute API,否则我的示例与 the docs 中给出的示例相同.我不确定为什么会遇到问题,如有任何帮助,我们将不胜感激!


完整错误:

{ 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' } ] }

最佳答案

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

const {google} = require('googleapis');
const key = require('./privateKey.json')
const packageName = "com.company.example"

let client = new google.auth.JWT(
key.client_email,
undefined,
key.private_key,
['https://www.googleapis.com/auth/androidpublisher']
)
const androidApi = google.androidpublisher({
version: 'v3',
auth: client
})

async function getApksList() {
let authorize = await client.authorize();
//insert edit
console.log('authorize :', authorize);
let res = await androidApi.edits.insert({
packageName: packageName
})
//get edit id
let editId = res.data.id
const res = await androidApi.edits.apks.list({
editId: editId,
packageName: packageName
});
console.log(`Result ${(JSON.stringify(res))}`);
}
getApksList().catch(console.error);

关于google-api - 即使在服务器到服务器身份验证后也无法使用 Google Play Developer API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566862/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com