2012-09-26 2 views
5

간단히 말해서 : 놀이터에 앱을 제공하지 않고 서버 측의 Google Play Android 개발자 API로 작업 할 수 있습니까?서버 측에서 google android 구독 확인하기

배경 : 월 단위 구독으로 앱을 제공하는 프로젝트를 진행 중입니다. 각 구독 (구매 토큰, 날짜 등)의 해당 데이터는 백엔드 데이터베이스에 저장됩니다. 이제 각각의 데이터 세트를 반복하는 cronjob을 만들고 싶습니다. 그리고 각 구독마다 구독이 여전히 유효한지 정보를 검색하기 위해 Google API에 연락하고 응답 된 데이터에 해당하는 데이터베이스를 업데이트하고 싶습니다. 지위.

백엔드 로직의 경우 google-api-java-client library을 사용합니다.

구독을 취소하거나 확인하려면 이전에 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 : 주어진 계정 범위가 유효한지 모르겠다. 단지 몇 가지 예에서이 값을 찾을 수 있었지만 this overview이나 google playground

$ 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 console -> API Access

    $ (2)로부터 : 나는 가입의 구매 상태를 확인하려고 다음 단계에서

사실 옆의 androidpush API가 not allow contacting it via service accounts, but only via web server applications auth flow 않습니다, get-method 매개 변수를 삽입 할 단서가 없습니다. https://developers.google.com/android-publisher/v1/purchases/get

질문

  • 패키지 이름은 무엇이며, 이러한 맥락에서 subscriptionId은 무엇인가 : 여기

    는 API입니까?
  • 어디서이 값을 가져 오거나 설정합니까?

this document을 읽은 후에 가짜/정적 응답을 처리 할 수있는 방법이 있다는 것을 알고 있습니다. 구독에도 가능하거나 모바일 장치에서만 인앱 결제를 사용할 수있는 곳이라면 어디에서나 읽을 수 있습니다.

어쨌든 이유가 궁금합니다/또는 샌드 박스 또는 s.th로 개발하기 쉬운 방법이 있다면 궁금합니다. simliar.

나는 아직도 일이 어떻게 작동해야 하는지를 이해하기 위해 큰 부분을 놓치고 있다는 느낌이 들었다. 어쩌면 누군가가 나에게이 장소에서 진행하는 방법에 대한 힌트를 줄 수도 있고, 내가 틀린 곳에서 나를 말할 수도 있습니다.

종류와 관련,

크리스토퍼

+0

확인 http://stackoverflow.com/a/24264696/165708 더 나은 솔루션. –

답변

4

나는 지금 내 이전의 이해 문제의 대부분을 알아낼 수 있습니다.

= 1 서버 webflow는 고객이 현재 URL은 (1) 생성 호출해야 androidpublisher API 작동하지 않기 때문에 = 수동 AUTHORIZATION의 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 =

를 인증 GENERATE .

= 3 = 콜백

구글 콜백은 다음과 같이 처리한다. 콜백에는 "code"매개 변수가 포함되어 있습니다.

= 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 = 요청 REFRESH 토큰

// 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")