gpt4 book ai didi

google-api - 使用Google Play开发者API进行服务器端授权?

转载 作者:行者123 更新时间:2023-12-04 22:40:07 25 4
gpt4 key购买 nike

需要授权才能从Google Play Developer API获取信息。

我知道如何用Postman做到这一点,但是实现授权要麻烦得多(重定向URL,处理重定向等)。
当您已经在Google Developer API Console中设置了auth数据时,将使用这些步骤。

1.) GET https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=http://www.myurl.com/oauth2callback&client_id=1234567890.apps.googleusercontent.com
2.) get code which was sent to redirect url.
3.) POST https://accounts.google.com/o/oauth2/token
with
grant_type:authorization_code
code:[the code I got before]
client_id:1234567890.apps.googleusercontent.com
client_secret:[my client secret]
4.) Invoke GET https://www.googleapis.com/androidpublisher/v2/applications/packageName/purchases/subscriptions/subscriptionId/tokens/token
with:
Scope: https://www.googleapis.com/auth/androidpublisher
and:
access_token as query parameter I got before.

现在,我想以编程方式完成所有这些操作。显然不是那么容易。我以为 Google API Client Libraries会有所帮助,但我不知道这些lib如何能帮助我解决我的用例。
例如,像 GoogleAuthorizationCodeFlow之类的类在请求时希望有一个用户ID,但现在不一定要有一个用户ID,因此我想知道如何以一种干净的方式使用此API。

有没有一种干净的方法可以通过一些API来更轻松/以编程方式处理OAuth2.0,以访问Google Play开发者API?否则,我必须手动实现它。

最佳答案

经过很多头痛之后(就像总是使用Google API和服务一样),我弄清楚了如何使用现有的API访问Google Play开发者API信息(例如帐单)。

1.)在开发人员API控制台中创建服务帐户(JSON) key :
enter image description here

2.)下载此service-account-private-key.json文件(不要将其与OAuth2.0客户端密码文件混淆!)。

3.)在Google Play开发者控制台中,转到Settings -> Users & Permissions -> Invite New User,然后将下载文件中的 client_email 设置为新用户的用户电子邮件。通过此 View 内的复选框分配要授予该用户的访问权限(例如“查看财务数据”)。

4.)在您的项目中添加适当的依赖项(版本...- 1.23.0对我不起作用):

<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-androidpublisher</artifactId>
<version>v2-rev50-1.22.0</version>
</dependency>

5.)将service-account-private-key.json文件加载到您的应用程序中。在我的情况下,它是一个网络服务器:
@Singleton
@Startup
public class WebserverConfiguration
{
private String serviceAccountPrivateKeyFilePath;

/** Global instance of the HTTP transport. */
public static HttpTransport HTTP_TRANSPORT;

/** Global instance of the JSON factory. */
public static JsonFactory JSON_FACTORY;

private GoogleCredential credential;

@PostConstruct
public void init()
{
assignServiceAccountFileProperty();
initGoogleCredentials();
}

public String getServiceAccountPrivateKeyFilePath()
{
return serviceAccountPrivateKeyFilePath;
}

public GoogleCredential getCredential()
{
return credential;
}

private void initGoogleCredentials()
{
try
{
newTrustedTransport();
newJsonFactory();

String serviceAccountContent = new String(Files.readAllBytes(Paths.get(getServiceAccountPrivateKeyFilePath())));
InputStream inputStream = new ByteArrayInputStream(serviceAccountContent.getBytes());

credential = GoogleCredential.fromStream(inputStream).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));

}
catch (IOException | GeneralSecurityException e)
{
throw new InitializationException(e);
}
}

private void newJsonFactory()
{
JSON_FACTORY = JacksonFactory.getDefaultInstance();
}

private void assignServiceAccountFileProperty()
{
serviceAccountPrivateKeyFilePath = System.getProperty("service.account.file.path");
if (serviceAccountPrivateKeyFilePath == null)
{
throw new IllegalArgumentException("service.account.file.path UNKNOWN - configure it as VM startup parameter in Wildfly");
}
}

private static void newTrustedTransport() throws GeneralSecurityException, IOException
{
if (HTTP_TRANSPORT == null)
{
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
}
}
}

6.)现在,我可以获取Google Play开发者API信息,例如评论:
private void invokeGoogleApi() throws IOException
{
AndroidPublisher publisher = new AndroidPublisher.Builder(WebserverConfiguration.HTTP_TRANSPORT, WebserverConfiguration.JSON_FACTORY, configuration.getCredential()).setApplicationName("The name of my app on Google Play").build();
AndroidPublisher.Reviews reviews = publisher.reviews();
ReviewsListResponse reviewsListResponse = reviews.list("the.packagename.of.my.app").execute();
logger.info("review list response = " + reviewsListResponse.toPrettyString());
}

这行得通。

我尚无法测试,但是我敢肯定,获取帐单信息也可以:
private SubscriptionPurchase getPurchase() throws IOException
{
AndroidPublisher publisher = new AndroidPublisher.Builder(WebserverConfiguration.HTTP_TRANSPORT, WebserverConfiguration.JSON_FACTORY, configuration.getCredential()).setApplicationName("The name of my app on Google Play").build();
AndroidPublisher.Purchases purchases = publisher.purchases();

SubscriptionPurchase purchase = purchases.subscriptions().get("the.packagename.of.my.app", "subscriptionId", "billing token sent by the app").execute();

//do something or return
return purchase;
}

关于google-api - 使用Google Play开发者API进行服务器端授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176187/

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