Am I getting the steps right for verifying a user's Android in-app subscription?

前端 未结 7 1468
刺人心
刺人心 2020-11-28 21:06

I am making an app that does not require a user account/login, and allows the user to purchase a subscription. I want to use the Google Play Developer API to verify whether

7条回答
  •  广开言路
    2020-11-28 21:59

    If you are like me, and want to do this in PHP, here is the procedure how to do it... Thanks to Kalina's answer it took me only three days to work out how it works :).

    Here goes:

    1. go to google developers console https://console.developers.google.com/ and create a web app. Put 'developers.google.com/oauthplayground'as a "redirect URI"; You will use it in step 2. You will get a client id and client secret when you create the account. Make sure you have the Google Play Android Developer API added.

    2. go to the Google oauth2 playground https://developers.google.com/oauthplayground/. This great tool is your best friend for the next few days. Now go to settings : make sure Use your own OAuth credentials is set. Only then you can fill in your client ID and client secret in the form below.

    3. In Google oauth2 playground go to step 1 Select & authorize APIs fill in the scope in the input field https://www.googleapis.com/auth/androidpublisher. I couldnt find the Google Play Android Developer API in the list, maybe they will add some time later. Hit AUTORIZE APIS. Do the authorisation thing that follows.

    4. In Google oauth2 playground go to step 2 Exchange authorization code for tokens. If all went well you will see a authorization code starting with /4. If something didnt go well check the error message on the right. Now you hit 'refresh access token'. Copy the Refresh token... it will start with /1...

    5. Now you can always get an access token! here is how:

      $url ="https://accounts.google.com/o/oauth2/token";
      $fields = array(
         "client_id"=>"{your client id}",
         "client_secret"=>"{your client secret}",
         "refresh_token"=>"{your refresh token 1/.....}",
         "grant_type"=>"refresh_token"
      );
      
      $ch = curl_init($url);
      
      //set the url, number of POST vars, POST data
      curl_setopt($ch, CURLOPT_POST,count($fields));
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      //execute post
      $lResponse_json = curl_exec($ch);
      
      //close connection
      curl_close($ch);
      

    Now you have an ACCESS TOKEN hooray... the JSON will look like this:

    "access_token" : "{the access token}",  "token_type" : "Bearer",  "expires_in" : 3600
    

    Finally you're ready to ask google something! Here is how to do it:

    $lAccessToken = "{The access token you got in}" ;
    $lPackageNameStr = "{your apps package name com.something.something}";
    $lURLStr =  "https://www.googleapis.com/androidpublisher/v1.1/applications/$lPackageNameStr/subscriptions/$pProductIdStr/purchases/$pReceiptStr";
    
    $curl = curl_init($lURLStr);
    
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    $curlheader[0] = "Authorization: Bearer " . $lAccessToken;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
    
    $json_response = curl_exec($curl);
    curl_close($curl);
    
    $responseObj = json_decode($json_response,true);
    

    The JSON returned will contain two timestamps, the initiationTimestampMsec and validUntilTimestampMsec the time the subscription is valid. Both are the nr of millisecs to add to the date 1/1/1970!

提交回复
热议问题