title: Java based Google App Engine, Android and authentication oauth2
body: <p>Authentication and app engine, there is a lot to be read about it, but a lot seems to be outdated!</p>
  
  <p>Even the google pages <a href="https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls" rel="nofollow">https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls</a></p>
  
  <p>Here, they talk about 'GoogleAccountCredential.usingAudience', but nowadays, you should use GoogleAuthUtil (as far as I know, please correct me if I'm wrong).</p>
  
  <p>I am trying to set up an app engine as a backend to my Android app (and in future, my iOS app).</p>
  
  <p>I am using Android Studio, used the 'new module' and chose app engine with cloud messaging there.</p>
  
  <p>I created a simple endpoint, and have a function there, here is some code:</p>
  
  <pre><code>public class ReviewEndpoint {
  
  // Make sure to add this endpoint to your web.xml file if this is a web application.
  
  private static final Logger LOG = Logger.getLogger(ReviewEndpoint.class.getName());
  
  /**
   * This method gets the &lt;code&gt;Review&lt;/code&gt; object associated with the specified         &lt;code&gt;id&lt;/code&gt;.
   * @param id The id of the object to be returned.
   * @return The &lt;code&gt;Review&lt;/code&gt; associated with &lt;code&gt;id&lt;/code&gt;.
   */
  @ApiMethod(name = "getReview")
  public Review getReview(@Named("id") Long id) {
      // Implement this function
      Review r = new Review();
      r.setData("test!");
  </code></pre>
  
  <p>As you can see, this is nicely generated by Android Studio. I implemented some stuf like creating the 'review' object and return it at the end.</p>
  
  <p>On the Android side, I can do this:</p>
  
  <pre><code>ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
  ReviewEndpoint ep = b.build();
  Review review = ep.getReview(1L).execute();
  data = review.getData();
  </code></pre>
  
  <p>and yes, I get 'test!' :)</p>
  
  <p>Now, I want to have this authenticated. I want to know which user wrote what, so I thought I am going to use GMail account and Facebook later.</p>
  
  <p>Here I'm stuck. I am able to get a token from the user on Android:</p>
  
  <pre><code>token = GoogleAuthUtil.getToken(MainScreenActivity.this, mAccount.name, "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile");
  </code></pre>
  
  <p>then you are able to add this token as credential to the request:</p>
  
  <pre><code>Credential cr = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(token);
  ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), cr);
  </code></pre>
  
  <p>Then in the app engine I tried to get the user info, but how?
  Will it be supplied as 'bearer'? How do I get this bearer token? Should I then do API request to get the data on the server?</p>
  
  <p>this does not work:</p>
  
  <pre><code>OAuthService service = OAuthServiceFactory.getOAuthService();
  try {
      User user = service.getCurrentUser();
  </code></pre>
  
  <p>can anyone give me a heads up?</p>