voscausa
voscausa

Reputation: 11706

How to use Oauth2 service accounts with Google Drive and the App Engine SDK

Accessing Google Drive with App Engine and service accounts works fine. To make it work, I also had to share my Drive files with the Service Account Name: [email protected].

But when I use the GAE SDK I receive a 401. I think the reason is the service_account name of the SDK for my shares. The service account name of the SDK is: test@localhost according to :

app_identity.get_service_account_name()  # returns test@localhost

The error message in the SDK console :

An error occurred: https://www.googleapis.com/drive/v2/files/0B9-mE....................nM/children?alt=json&key=AI........................N0 returned "Invalid Credentials">

This is the code to access Google Drive :

def get(self):

    SCOPE = 'https://www.googleapis.com/auth/drive'
    credentials = AppAssertionCredentials(scope=SCOPE)
    logging.info(app_identity.get_service_account_name())
    http = credentials.authorize(httplib2.Http())
    if os.environ['SERVER_SOFTWARE'].startswith('Development') : 
        service = build('drive', 'v2', http=http, developerKey='...API key...')
    else :
        service = build('drive', 'v2', http=http, developerKey='...APY key...')
    # drive requests        

UPDATE :

My personal opinion : This can only work with GAE and not with the SDK, because I cannot make a Drive share with the SDK service_account name. Or is it possible to specify another service account name.

Upvotes: 3

Views: 2891

Answers (1)

Ali Afshar
Ali Afshar

Reputation: 41643

Please check the documentation for Application-owned service accounts, and there is also a video you can watch too.

The docs contain Python sample code.

Upvotes: 2

Related Questions