Bev*_*vor 4 google-api google-oauth google-oauth-java-client google-oauth2
需要获得授权才能从Google Play开发者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.
Run Code Online (Sandbox Code Playgroud)
现在,我要以编程方式完成所有这些操作。显然不是那么容易。我以为Google API客户端库会有所帮助,但我看不到这些库如何为我的用例提供帮助。
例如,像GoogleAuthorizationCodeFlow之类的类在请求时希望有一个用户ID,但现在不一定要有一个用户ID,因此我想知道如何以一种干净的方式使用此API。
有没有一种干净的方法可以通过一些API来更轻松/以编程方式处理OAuth2.0,以访问Google Play开发者API?否则,我必须手动实现它。
经过很多头痛之后(就像总是使用Google API和服务一样),我弄清楚了如何使用现有的API访问Google Play开发者API信息(例如帐单)。
1.)在开发人员API控制台中创建服务帐户(JSON)密钥:

2.)下载此service-account-private-key.json文件(不要将其与OAuth2.0客户端密码文件混淆!)。
3.)在Google Play开发者控制台中,转到下载的文件中Settings -> Users & Permissions -> Invite New User的client_email并将其设置为新用户的用户电子邮件。通过此视图内的复选框分配要授予此用户的访问权限(例如“查看财务数据”)。
4.)向您的项目中添加适当的依赖项(版本...- 1.23.0对我不起作用):
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-androidpublisher</artifactId>
<version>v2-rev50-1.22.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
这工作了。
我尚无法测试,但是我确定获取帐单信息也可以:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3923 次 |
| 最近记录: |