Checking google android subscriptions from server side

后端 未结 1 1425
花落未央
花落未央 2021-02-06 14:27

in a nutshell: Can I work with the Google Play Android Developer API from server-side without providing any app in the play store?

Background: I\'m working on a project

1条回答
  •  失恋的感觉
    2021-02-06 15:06

    I could now figure out most of my previous understanding problems.

    =1= GENERATE 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= AUTHENTICATE

    Since the server-webflow is not working for the androidpublisher API the customer must now call the URL generated in (1) manually.

    =3= CALLBACK

    The google callback should process the next steps. The callback contains the parameter "code" which we have to use.

    =4= REQUEST AUTH-TOKEN

        // Build the HTTP parameter
        Map 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= REQUEST REFRESH TOKEN

        // Build the HTTP parameter
        Map 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")
    

    0 讨论(0)
提交回复
热议问题