Search code examples
google-app-enginegoogle-appsgoogle-drive-apigoogle-apps-marketplace

Google Drive Access using AuthSubUtil.exchangeForSessionToken


On Google AppEngine i have been through authentication for Google Docs ... access using AuthSub authshub.

We managed to AuthSubUtil.exchangeForSessionToken(..).

QUESTION: Is it possible to follow up and get Access to Google Drive using this token?

... new Drive.Builder(httpTransport, jsonFactory, credential);


Solution

  • I'm not sure if AuthSub works with the Google Drive API, but if it does, this code snippet should resolve your problem:

    new Drive.Builder(httpTransport, jsonFactory, new HttpRequestInitializer() {
      @Override
      public void initialize(HttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        // Not sure if this test is necessary as headers might never be null.
        if (headers == null) {
          headers = new HttpHeaders();
          request.setHeaders(headers);
        }
        headers.setAuthorization("AuthSub token=\"<TOKEN>\"");
      }
    }).build();
    

    Important: please be aware that AuthSub is being deprecated and you should try to migrate your application to use OAuth 2.0 as soon as you can.