5

一言で言えば:Playストアにアプリを提供せずにサーバー側からGoogle Play Android Developer APIを使用できますか?

背景:アプリに月額サブスクリプションを提供するプロジェクトに取り組んでいます。各サブスクリプションの対応するデータ(購入トークン、日付など)は、バックエンドデータベースに保存されます。次に、これらの各データセットを反復処理するcronジョブを作成します。サブスクリプションごとに、Google APIに連絡して、サブスクリプションがまだ有効かどうかの情報を取得し、応答されたステータスに対応するデータベースを更新します。 。

バックエンドロジックには、google-api-java-clientライブラリを使用します。

サブスクリプションをキャンセルまたは確認するには、前にOAuth2で自分自身を認証する必要があります。そこに行って、それをしました。

new GoogleCredential.Builder()
    .setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
    .setServiceAccountScopes("https://www.googleapis.com/auth/androidpublisher") // $1
    .setServiceAccountPrivateKeyFromP12File(new File(filePath))
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET) // $2
    .build();

$ 1:指定されたアカウントスコープが有効かどうかわかりません。ほんの数例でこの値を見つけることができたのですが、この概要でもグーグルの遊び場でも見つかりませんでした

$ 2この情報を提供していない例をたくさん見つけましたが、これは必要だと思います。

しかし、残念ながら、無効なデータ(間違った電子メールや秘密鍵など)を提供した場合、違いはわかりません。

質問

  • GoogleCredentialが正しいことを確認するにはどうすればよいですか?
  • androidpublisher APIに連絡するなど、次のステップでそれを確認できますか?

次のステップでは、サブスクリプションの購入ステータスを取得しようとします。

Androidpublisher publisher = new Androidpublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                               .setApplicationName(GOOGLE_PRODUCT_NAME) // $1                
                               .build();
Androidpublisher.Purchases purchases = publisher.purchases();
Androidpublisher.Purchases.Get get = purchases.get("android.test.purchased", "monthly001", "mytoken"); // $2
SubscriptionPurchase subscripcion = get.execute(); 

$ 1:APIコンソールからのダミー製品名->APIアクセス

$ 2:androidpush APIは、サービスアカウント経由での接続を許可していませんが、Webサーバーアプリケーションの認証フロー経由でのみ接続を許可しているという事実に加えて、get-メソッドのパラメーターに何を挿入するかについての手がかりがありません。

APIは次のとおりです: https ://developers.google.com/android-publisher/v1/purchases/get

質問

  • このコンテキストでのパッケージ名とsubscriptionIdは何ですか?
  • これらの値はどこで取得/設定できますか?

このドキュメントを読んだ後、私は偽の/静的な応答に対処する方法があることを知っています。しかし、これがサブスクリプションでも可能である場合、またはモバイルデバイスでのアプリ内請求のみで可能である場合、私はどこにも読むことができません。

とにかく、サンドボックスやs.thで開発する簡単な方法があるのはなぜか/あるのだろうかと思います。似ている。

物事がどのように機能するかを理解するための大きな部分が欠けているだけだと今でも感じています。たぶんあなたの誰かが私にこの場所で進む方法のヒントを与えるか、私が間違っているところを私に言うかもしれません。

敬具、

クリストファー

4

1 に答える 1

4

これで、以前の理解の問題のほとんどを理解することができました。

=1=認証URLを生成

String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(googleClientId,callbackUrl,"https://www.googleapis.com/auth/androidpublisher").build()    
// See why: http://stackoverflow.com/questions/8433990/when-authenticating-with-oauth-and-youtube-always-get-error-invalid-grant-on
authorizeUrl += "&approval_prompt=force&access_type=offline"

=2=認証

server-webflowはandroidpublisherAPIで機能していないため、顧客は(1)で生成されたURLを手動で呼び出す必要があります。

=3=コールバック

Googleコールバックは次のステップを処理する必要があります。コールバックには、使用する必要のあるパラメータ「コード」が含まれています。

= 4=AUTH-TOKENをリクエストする

    // Build the HTTP parameter
    Map<String,String> params = [:]
    params.put("grant_type", "authorization_code")
    params.put("code", code.encodeAsURL())
    params.put("client_id", customer.googleClientId.encodeAsURL())
    params.put("client_secret", customer.googleClientSecret.encodeAsURL())
    params.put("redirect_uri", getCallbackUrl().encodeAsURL())

    // Send the POST request
    // This action might throw an exception in case any parameter were wrong, invalid or not specified.
    String result = HttpRequestHandler.sendRequest("https://accounts.google.com/o/oauth2/token", params);
    JSONElement jsonResult = JSON.parse(result)

    // Map result
    OAuth2Result oAuth2Result = new OAuth2Result()
    oAuth2Result.accessToken = jsonResult.getAt("access_token")
    oAuth2Result.refreshToken = jsonResult.getAt("refresh_token")
    oAuth2Result.ttlSeconds = Integer.parseInt(jsonResult.getAt("expires_in").toString())
    oAuth2Result.tokenType = jsonResult.getAt("token_type") 

=5=リフレッシュトークンをリクエストする

    // Build the HTTP parameter
    Map<String,String> params = [:]
    params.put("grant_type", "refresh_token")
    params.put("refresh_token", this.customer.googleRefreshToken.encodeAsURL())
    params.put("client_id", customer.googleClientId.encodeAsURL())
    params.put("client_secret", customer.googleClientSecret.encodeAsURL())

    // Send the POST request
    // This action might throw an exception in case any parameter were wrong, invalid or not specified.
    String result = HttpRequestHandler.sendRequest("https://accounts.google.com/o/oauth2/token", params);
    JSONElement jsonResult = JSON.parse(result)

    // Map result
    OAuth2Result oAuth2Result = new OAuth2Result()
    oAuth2Result.accessToken = jsonResult.getAt("access_token")
    oAuth2Result.refreshToken = jsonResult.getAt("refresh_token")
    oAuth2Result.ttlSeconds = Integer.parseInt(jsonResult.getAt("expires_in").toString())
    oAuth2Result.tokenType = jsonResult.getAt("token_type")
于 2012-10-08T10:58:18.473 に答える