<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[mbaasy - Medium]]></title>
        <description><![CDATA[mbaasy - Medium]]></description>
        <link>https://medium.com/mbaasy?source=rss----4102fb574628---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>mbaasy - Medium</title>
            <link>https://medium.com/mbaasy?source=rss----4102fb574628---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 07:22:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/mbaasy" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Google Play Android Developer API ]]></title>
            <link>https://medium.com/mbaasy/google-play-android-developer-api-c86af846e0a5?source=rss----4102fb574628---4</link>
            <guid isPermaLink="false">https://medium.com/p/c86af846e0a5</guid>
            <dc:creator><![CDATA[Marc Greenstock]]></dc:creator>
            <pubDate>Fri, 03 Feb 2017 22:36:17 GMT</pubDate>
            <atom:updated>2014-06-19T19:22:51.651Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*24C1NbTE5SgKtRGTYXdQbQ.png" /></figure><h4>How to authenticate </h4><p>Recently I worked on an API that provides support for an Android app that has in-app purchases, and I needed a way to validate these purchases. Google provides an <a href="https://developers.google.com/android-publisher/v1_1/">API for purchase validation</a> but there are a few details Google assumes you know already when using it.</p><h3>Ensure you have the right permissions</h3><p>Firstly you will want access to the <a href="https://play.google.com/apps/publish/">Google Play Console</a>. If you are the owner you can skip this part. Otherwise, make sure you have access to <strong>financial information</strong>, the owner is the only user who can set this for you. Without this set you will receive a <strong>403 forbidden</strong> when making your requests.</p><p>Next, log into your <a href="https://console.developers.google.com/">Google Developers Console</a>, and create a project. In the <strong>API &amp; AUTH &gt; APIs</strong> section, switch on <strong>Google Play Android Developer API</strong>.</p><p>Next in the <strong>API &amp; AUTH &gt; Consent Screen</strong> select an <strong>Email Address </strong>and enter a <strong>Project Name</strong>. Unless you are using google OAuth in your app or elsewhere for this project then your users will never see this so put what ever you like, and the rest of the details don’t matter.</p><p>Now go to <strong>API &amp; AUTH &gt; Credentials</strong> and create a new OAuth Client ID. Select Web Application (I know you’re not making a web application but for some reason Google thinks this was a good idea), enter <em>http://localhost</em> in <strong>Authorized JavaScript Origins </strong>and<strong> </strong><em>http://localhost/oauth_callback</em> in <strong>Authorized Redirect URI</strong>. This end point doesn’t matter too much, it’s only somewhere Google can redirect back to when setting up your credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_0Bo5Ttm0d7WO2YCJQ1ZHw.png" /></figure><p>Now you have this set up you can start pre-authenticating.</p><h3>Pre-authentication</h3><pre>https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&amp;response_type=code&amp;access_type=offline&amp;redirect_uri=http://localhost/oauth_callback&amp;client_id=YOUR_CLIENT_ID</pre><p>Copy and paste the above URI in your browser replacing <strong>YOUR_CLIENT_ID</strong> with the <strong>Client ID</strong> you got in the last step.</p><p>You will be taken to an authentication screen asking you if you allow your Project to have access to <strong>Google Play Android Developer</strong>, say yes. You’ll then be redirected to <em>http://localhost/oauth_callback?code=</em>authorization_code<a href="http://localhost/oauth_callback?code=AUTHORIZATION_CODE.">.</a> You will probably get some kind of error, 404 or whatever, but that doesn’t matter, the important part is the <strong>authorization_code</strong> in the query string. This is necessary to complete the next step.</p><p>Here you will swap the <strong>authorization_code</strong> for a <strong>google_refresh_token</strong>.</p><pre>code = &quot;authorization_code&quot;<br>client_id = &quot;client_id&quot;<br>client_secret = &quot;client_secret&quot;<br>redirect_uri = &quot;http://localhost/oauth_callback&quot;</pre><pre>response = HTTParty<strong>.</strong>post(&#39;https://accounts.google.com/o/oauth2/token&#39;, {<br> body: {<br>   grant_type: &#39;authorization_code&#39;,<br>   code: code,<br>   client_id: client_id,<br>   client_secret: client_secret,<br>   redirect_uri: redirect_uri<br> }<br>})</pre><pre>JSON.parse(response.body)</pre><p>The code above is written in Ruby, but it’s a simple HTTP Post request, you can use any language or library or even a REST client like <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?utm_source=chrome-ntp-icon">Postman</a> for Chrome. The first 4 lines in the code above will need to be changed to your respective values.</p><p>In your response you should get a <strong>refresh_token</strong>, if you don’t then it is because you have used the <strong>authorization_code</strong> already and you will need to go to your <a href="https://security.google.com/settings/security/permissions?pli=1">Google Account Permissions</a> page, revoke access to your project, and repeat the Pre-authentication process.</p><p>If you have your <strong>refresh_token</strong> you’re ready to proceed to the next step.</p><h3>Getting an access_token</h3><p>Okay still with me? Good, we’re nearly there.</p><p>Now that you have your <strong>refresh_token</strong> you can now get your <strong>access_token</strong> and keep it fresh. <strong>access_token</strong>’s expire after a while so you’ll need to use your <strong>refresh_token</strong> to get a new one after it expires and I doubt you’ll want to do this manually.</p><pre>client_id = &quot;client_id&quot;<br>client_secret = &quot;client_secret&quot;<br>refresh_token = &quot;refresh_token&quot;</pre><pre>response = HTTParty<strong>.</strong>post(&#39;https://accounts.google.com/o/oauth2/token&#39;, {<br> body: {<br>   grant_type: &#39;refresh_token&#39;,<br>   client_id: client_id,<br>   client_secret: client_secret,<br>   refresh_token: refresh_token<br> }<br>})</pre><p>The code above, while similar to the last snippet will exchange your <strong>refresh_token</strong> for an <strong>access_token</strong> and an <strong>expires_in</strong> integer. You may want to store your access_token in a key value store that can expire, e.g. Redis.</p><pre>if response.message == ‘OK’ &amp;&amp; <br>  (json = JSON.parse(response.body))<br>  redis.set(‘access_token’, json[‘access_token’])<br>  redis.expireat(‘access_token’, Time.now.to_i + json[‘expires_in’])<br>  access_token = json[‘access_token’]<br>else<br>  raise &quot;Oh my god there is no access token&quot;<br>end</pre><p>The above code is a continuation, taking response, parsing it then storing the <strong>access_token</strong> in Redis and setting an expiry based on <strong>expires_in</strong> and the current time. You’ll want to put this in a setter method so that you can set the access token when it doesn’t exist in Redis and a getter method that will retrieve the access token when it does exist.</p><p>Now you should have a nice way of refreshing your <strong>access_token</strong>, you can move on to actually validating purchases.</p><h3>Validating Purchases</h3><p>Yeah I lied when I said we were nearly there, but this time I promise.</p><p>Google Play has two kinds of in-app purchases; <a href="https://developers.google.com/android-publisher/v1_1/purchases">purchases</a> and <a href="https://developers.google.com/android-publisher/v1_1/inapppurchases">inapppurchases</a>. The former is subscriptions (I do not know why they just didn’t call them subscriptions) and the latter is single purchases. Anyway, now you have the <strong>access_token</strong> token all you need to do is your requests to validate with your <strong>access_token</strong> as a query param, e.g.</p><pre>https://www.googleapis.com/androidpublisher/v1.1/applications/PACKAGE_NAME/subscriptions/SUBSCRIPTION_ID/purchases/TOKEN?access_token=ACCESS_TOKEN</pre><p>Of course you will need a PACKAGE_NAME, SUBSCRIPTION_ID and a TOKEN and there is no way to actually get a TOKEN unless you actually pay money for a TOKEN through the app you are developing.</p><h3>Troubleshooting</h3><p>If you get a <strong>403 forbidden</strong>, it is most probably because your Google Play Console account doesn’t have access to financial information. Have your account owner turn this on for you.</p><p>If you get a <strong>401 unauthorized</strong>, then it is because you missed a step above, go back and try again.</p><p>So this was my first post on Medium, and on this subject, if you notice any errors please let me know. I hope this helps you figuring out how this whole authentication mess works.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c86af846e0a5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mbaasy/google-play-android-developer-api-c86af846e0a5">Google Play Android Developer API </a> was originally published in <a href="https://medium.com/mbaasy">mbaasy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>