title: How to use the token returned by GoogleAuthUtil.getToken with my App Engine backend
body: <h1>What I want to do</h1>
  
  <p>I have a simple Google App Engine backend and a simple Android Application and I want to do authenticated requests from the Android App to the server. I read about Google Cloud Endpoints and even if it is a really good API, I feel that it's a bit overkill for what I want to do. I just want to do an authenticated HTTP request and get the response text.</p>
  
  <pre><code>GET myappid.appspot.com/api/user
  </code></pre>
  
  <p>Should answer:</p>
  
  <pre><code>Hello john.doe
  </code></pre>
  
  <p>If the user <em>john.doe@gmail.com</em> does the request.</p>
  
  <h1>Backend side:</h1>
  
  <p>I created a new App Engine project:</p>
  
  <pre><code>WEB_CLIENT_ID=123456789012.apps.googleusercontent.com
  </code></pre>
  
  <p>and registered an Android App ("Accessing APIs directly from Android"):</p>
  
  <pre><code>package name : com.myappid
  debug SHA1 fingerprint: 3a:e1:05:17:15:54:c6:c7:9b:ef:19:74:ae:5b:f7:0f:c3:d5:45:9d
  </code></pre>
  
  <p>And this created</p>
  
  <pre><code>ANDROID_CLIENT_ID=123456789012-9f4sd525df3254s3d5s40s441df705sd.apps.googleusercontent.com
  </code></pre>
  
  <h2>app.yaml</h2>
  
  <pre><code>application: myappid
  version: 1
  runtime: python27
  api_version: 1
  threadsafe: true    
  
  handlers:
  - url: /api/.*
      secure: always
      script: api.APP
  
  libraries:
  - name: webapp2
      version: latest
  - name: pycrypto
      version: latest
  </code></pre>
  
  <h2>api.py</h2>
  
  <pre><code>import webapp2
  from google.appengine.api import users
  from google.appengine.api import oauth
  
  class GetUser(webapp2.RequestHandler):
  
      def get(self):
          user = users.get_current_user()
          self.response.headers['Content-Type'] = 'text/plain'
          self.response.out.write('Hello, {}\n'.format('None' if user is None else user.nickname()))
          try:
              user = oauth.get_current_user()
              self.response.out.write('Hello OAuth, {}\n'.format('None' if user is None else user.nickname()))
          except Exception as e:
              self.response.out.write(str(e)+'\n')
  
  class SignIn(webapp2.RequestHandler):
  
      def get(self):
          if users.get_current_user() is None:
              self.redirect(users.create_login_url(self.request.uri))
  
  APP = webapp2.WSGIApplication([
      ('/api/user', GetUser),
      ('/api/signin', SignIn),
  ], debug = True)
  </code></pre>
  
  <h1>Android side</h1>
  
  <pre><code>public class MainActivity extends Activity
  {
      private static final String CLIENT_ID = "123456789012.apps.googleusercontent.com";
      private static final String SCOPE = "audience:server:client_id:" + CLIENT_ID;
      private static final int AUTH_REQUEST_CODE = 1;
      private Account mAccount;
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mAccount = AccountManager.get(mActivity).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE)[0];
          new GetAuthToken().execute(mAccount.name);
      }
  
      protected void log(String msg) {
          TextView tv = (TextView) mActivity.findViewById(R.id.textView);
          tv.setText(tv.getText() + "\n" + msg);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == AUTH_REQUEST_CODE) {
              if (resultCode == RESULT_OK) {
                  new GetAuthToken().execute(mAccount.name);
              }
          }
      }
  
      private class GetAuthToken extends AsyncTask&lt;String, Void, String&gt; {
          @Override
          protected String doInBackground(String... params) {
              try {
                  // Retrieve a token for the given account and scope. It will always return either
                  // a non-empty String or throw an exception.
                  String email = params[0];
                  String token = GoogleAuthUtil.getToken(mActivity, email, SCOPE);
                  return token;
              } catch (GooglePlayServicesAvailabilityException playEx) {
                  Dialog alert = GooglePlayServicesUtil.getErrorDialog(playEx.getConnectionStatusCode(), mActivity, AUTH_REQUEST_CODE);
                  return "error - Play Services needed " + playEx;
              } catch (UserRecoverableAuthException userAuthEx) {
                  // Start the user recoverable action using the intent returned by
                  // getIntent()
                  mActivity.startActivityForResult(userAuthEx.getIntent(), AUTH_REQUEST_CODE);
                  return "error - Autorization needed " + userAuthEx;
              } catch (IOException transientEx) {
                  // network or server error, the call is expected to succeed if you try again later.
                  // Don't attempt to call again immediately - the request is likely to
                  // fail, you'll hit quotas or back-off.
                  return "error - Network error " + transientEx;
              } catch (GoogleAuthException authEx) {
                  // Failure. The call is not expected to ever succeed so it should not be
                  // retried.
                  return "error - Other auth error " + authEx;
              }
          }
  
          @Override
          protected void onPostExecute(String result) {
              if (result.startsWith("error -")) {
                  log(result);
              } else {
                  log("Obtained token : " + result);
                  new GetAuthedUserName().execute(result);
              }
          }
      }
  
      private class GetAuthedUserName extends AsyncTask&lt;String, Void, String&gt; {
          @Override
          protected String doInBackground(String... params) {
              try {
                  String token = params[0];
                  URL url = new URL("https://myappid.appspot.com/api/user");
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  //conn.setRequestProperty("Authorization", "Bearer " + token);
                  conn.addRequestProperty("Authorization",  "OAuth " + token);
                  InputStream istream = conn.getInputStream();
                  try {
                      BufferedReader reader = new BufferedReader(new InputStreamReader(istream));
                      String line;
                      StringBuilder sb = new StringBuilder();
                      while ((line = reader.readLine()) != null) {
                          sb.append(line);
                      }
                      return sb.toString();
                  } catch (IOException e) {
                      return "error - Unable to read from the connection";
                  }
              } catch (MalformedURLException e) {
                  return "error - Malformed URL " + e;
              } catch (IOException e) {
                  return "error - IO error " + e;
              }
          }
  
          @Override
          protected void onPostExecute(String result) {
              if (result.startsWith("error -")) {
                  log(result);
              } else {
                  log("Request result : " + result);
              }
          }
      }
  }
  </code></pre>
  
  <h1>What works</h1>
  
  <p>I can use my browser, to to</p>
  
  <pre><code>https://myappid.appspot.com/api/signin
  </code></pre>
  
  <p>login as John Doe and then</p>
  
  <pre><code>https://myappid.appspot.com/api/user
  </code></pre>
  
  <p>And I get</p>
  
  <pre><code>Hello, john.doe
  </code></pre>
  
  <p>Fantastic it's exactly what I expect.</p>
  
  <h1>What doesn't work</h1>
  
  <p>With Android, I all my tries resulted in </p>
  
  <pre><code>Hello, None
  </code></pre>
  
  <p>As you can see in the Android code, I use GoogleAuthUtil to retrieve a token but I don't really understand what I'm supposed to do with it.</p>
  
  <pre><code>String token = GoogleAuthUtil.getToken(mActivity, email, SCOPE);
  </code></pre>
  
  <p>Then I build the request:</p>
  
  <pre><code>URL url = new URL("https://myappid.appspot.com/api/user");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  </code></pre>
  
  <p>And add the "Authorization" header:</p>
  
  <pre><code>conn.setRequestProperty("Authorization", "Bearer " + token);
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>conn.addRequestProperty("Authorization",  "OAuth " + token);
  </code></pre>
  
  <p>There is probably something missing on Android or on the App Engine backend but I really don't get what.
  Is there a piece of API that simplifies this ?</p>
  
  <p>It seems so simple with a browser...</p>
  
  <p>TIA</p>