1

I am trying to build my own endpoints inside of an App Engine Application. There is an endpoint API that needs to ask user for "https://www.googleapis.com/auth/drive.readonly" scope. It performs a list of the Drive API and scan the drive file of that user.

The problem is that I don't know how to make call to Drive api inside of an endpoint API.

I think inside of the endpoint method, it has the credentials we got from the user. But I don't know how to receive that.

I am using python as the backend language.

@drivetosp_test_api.api_class(resource_name='report')
class Report(remote.Service):
@endpoints.method(EmptyMessage, EmptyMessage,
    name='generate',
    path='report/generate',
    http_method='GET'
    ) 
def report_generate(self, request):
    logging.info(endpoints)
    return EmptyMessage()
Frank Zhang
  • 127
  • 2
  • 12

1 Answers1

1

You can use os.environ to access the HTTP Authorization header, that includes the access token, that was granted all scopes you asked for on the client side, include drive.readonly in your sample.

if "HTTP_AUTHORIZATION" in os.environ:
  (tokentype, token)  = os.environ["HTTP_AUTHORIZATION"].split(" ")

You can then use this token to make calls to the API, either directly or by using the Google APIs Client Library for Python:

credentials = AccessTokenCredentials(token, 'my-user-agent/1.0')
http = httplib2.Http()
http = credentials.authorize(http)

service = build('drive', 'v2', http=http)

files = service.files().list().execute()

Note that this approach won't work if you are using an Android client, because that uses ID-Token authorization instead of access tokens.

Scarygami
  • 15,009
  • 2
  • 35
  • 24
  • Actually, currently I am using the following way to achieve. I think it is similar to your approach. basic_auth = self.request_state.headers.get('authorization'). Then use a normal http request to use Drive API with headers set to the basic_auth. request = urllib2.Request(request_url, headers={"Authorization" : basic_auth}). It is working. Which way would be better? – Frank Zhang Oct 07 '14 at 16:54
  • Yup, that works as well. Doesn't really make much difference. I myself find using the Google APIs Client Library easier/cleaner. Saves me the trouble of constructing the request_url manually. – Scarygami Oct 07 '14 at 20:29
  • Thanks. By the way, in order to use the approach in this answer, Google APIs Client Library should be downloaded and placed inside of the project. And use from oauth2client.client import AccessTokenCredentials to import AccessTokenCredentials. This comment is just for those who hit this question later and not sure how to import AccessTokenCredentials. – Frank Zhang Oct 08 '14 at 15:20
  • 1
    Is there any workaround when using an Android client? – hyotam Nov 24 '14 at 18:41