【发布时间】:2020-01-07 23:33:06
【问题描述】:
我正在尝试使用适用于 Java 的 Google API 客户端库来获取有关在我的 Android 应用中购买的用户订阅的信息。然后我得到了这个错误:
{
"code" : 401,
"errors" : [ {
"domain" : "androidpublisher",
"message" : "The current user has insufficient permissions to perform the requested operation.",
"reason" : "permissionDenied"
} ],
"message" : "The current user has insufficient permissions to perform the requested operation."
}
我正在尝试这些步骤:
- 转到https://console.cloud.google.com
- 创建项目(或选择现有项目)
- 创建具有 Pub/Sub Admin 角色的服务帐号
- 转到https://console.cloud.google.com/apis/library 并搜索“Google 玩 Android 开发者 API”
- 启用该 API
- 转到https://play.google.com/apps/publish
- 转到设置 > 开发者帐户 > API 访问
- 链接您在第 2 步中创建的项目
- 将显示您在第 3 步中创建的服务帐户
- 授予对 Play 控制台中应用的财务权限的访问权限
- 至少等待 24 小时,更改才会生效。
我正在尝试这段代码:
httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
String applicationName = "App name";
String packageName = "com.example.xxx";
final Set<String> scopes = Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER);
AssetManager am = getAssets();
InputStream inputStream = am.open("key1.p12");
File file = createFileFromInputStream(inputStream);
Log.d(TAG, "file : " + file);
if(file != null){
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 8) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("pp-subscription@practice-presto-dev.iam.gserviceaccount.com").setServiceAccountScopes(scopes)
//.setServiceAccountId("practice-presto-subscription@practice-presto-dev.iam.gserviceaccount.com").setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(file).build();
AndroidPublisher pub = new AndroidPublisher.Builder
(httpTransport, jsonFactory, credential)
.setApplicationName(applicationName)
.build();
final AndroidPublisher.Purchases.Subscriptions.Get get =
pub.purchases().subscriptions().get(packageName, "sub_monthly_pro", "pcehicpbjhcdnjockiniaokh.AO-J1OxsJtLehF3z_naoXR4LE0jqiXrABAPYiZMNRMZO5jnKI9gnyHmPP7INtcc2kyptNKP_HM6MjEPQfmYWmJ8R_geonsLqMXA9TLsozqNexh-FxSvQFDZSUTgBW_azvdAJPLxPFuKd");
final SubscriptionPurchase purchase = get.execute();
Log.d(TAG, "Found google purchase item " + purchase.toPrettyString());
}
}
我正在使用测试应用内购买模式。
我想在更新订阅计划后获取最新的订阅收据。
【问题讨论】:
标签: java android google-api-java-client android-inapp-purchase