title: How do I restrict Google App Engine Endpoints API access to only my Android applications?
body: <p>I am an Android developer building my first Google App Engine (java) back-end for my apps. I don't want anybody else to access this API other than my app. (I plan to use App engine for verifying InApp purchases in my Android app). My data is not relevant to users so, 
  I don't want users to be able to access my API even if they are logged in with their Google accounts (on web or Android devices).</p>
  
  <p>I followed the steps mentioned in - "Specifying authorized clients in the API backend" 
  (<a href="https://developers.google.com/appengine/docs/java/endpoints/auth" rel="nofollow noreferrer">https://developers.google.com/appengine/docs/java/endpoints/auth</a>) 
  like generating client IDs and add them in @Api (clientIds and audiences)
  except "Add a User parameter" - since I don't need user authentication.</p>
  
  <p>Then I deployed App engine and I am still able to access the API through API explorer (<a href="https://your_app_id.appspot.com/_ah/api/explorer" rel="nofollow noreferrer">https://your_app_id.appspot.com/_ah/api/explorer</a>)
  (I haven't added API_EXPLORER client ID)</p>
  
  <p>I tested with the APK that was built with the endpoint libs before adding client IDs and can still access the API.</p>
  
  <ul>
  <li><p>Is adding a "User parameter" to all endpoint APIs a must? to achieve my purpose (restrict API to only my Android apps).</p></li>
  <li><p>Can I pass null as userAccount name from Android client and ignore user parameter value on server (since it will be null)? Will this ensure that the API is accessible only from my android apps (since the client ID is generated for my package name and SHA1 of the APK?)</p></li>
  <li><p>Should I use something like a service account for this purpose?</p></li>
  </ul>
  
  <p>The documentation says for Android, both Android and Web client IDs must be added and audience must be the same as web client ID. Does this open access to any other web client? can I skip mentioning web client ID and still achieve my purpose?</p>
  
  <p>Appreciate your time and help.</p>
  
  <p>...... updating with my further investigation ...</p>
  
  <p>I did the following:</p>
  
  <ul>
  <li><p>Added User parameter to APIs on backend - but did not check for null value. API can still be accessed without passing any credentials (from Android debug APK and API explorer)</p></li>
  <li><p>Then, I tried</p>
  
  <p>mCredential = GoogleAccountCredential.usingAudience(this, "server:client_id:" + WEB_CLIENT_ID);
  mCredential.setSelectedAccountName(null);</p></li>
  </ul>
  
  <p>and passed this credential to API builder (as suggested in some other posts)
  Caused FATAL EXCEPTION. So, we can't pass null account name.</p>
  
  <ul>
  <li><p>I could call the API using API explorer without OAuth. But when I enabled OAuth, it gave error saying this client ID is not allowed! ( I haven't yet added com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID in client_ids{})</p></li>
  <li><p>Then I added code to throw OAuthRequestException on the backend if the user is null. This resulted in API explorer getting errors without OAuth. It works with OAuth enabled after adding API_EXPLORER_CLIENT_ID to client_ids)</p></li>
  <li><p>Added code to pass valid user account name(email) from my Android app. Then, I am able to access API only with my release APK. Even the debug APK gets exceptions! - which is what I expected..So, I assume no other Android apps will be able to access this API.</p></li>
  </ul>
  
  <p>So, not checking for null user on back-end API is a bad idea (as suggested in other posts). It is as good as not mentioning any client_ids and not having User param.</p>
  
  <p>Only question I have at this moment is: If some one can figure out the WEB_CLIENT_ID from the APK, will they be able to use it to build a web client to access my API (I haven't mentioned client secret anywhere in the code. So I am thinking this is not possible).</p>
  
  <hr>
  
  <p>I did search Google groups and Stackoverflow, but still it is not clear.</p>
  
  <ul>
  <li><p>(Authenticate my “app” to Google cloud endpoints not a “user”) 
  <a href="https://stackoverflow.com/questions/20396577/authenticate-my-app-to-google-cloud-endpoints-not-a-user">Authenticate my &quot;app&quot; to Google Cloud Endpoints not a &quot;user&quot;</a></p></li>
  <li><p>(How do I protect my API that was built using Google Cloud Endpoints?)
  <a href="https://stackoverflow.com/questions/16890175/how-do-i-protect-my-api-that-was-built-using-google-cloud-endpoints">How do I protect my API that was built using Google Cloud Endpoints?</a></p></li>
  <li><p>(Restrict access to google cloud endpoints to Android app)
  <a href="https://stackoverflow.com/questions/17873993/restrict-access-to-google-cloud-endpoints-to-android-app">Restrict access to google cloud endpoints to Android app</a></p></li>
  </ul>