Vito Azzollini
Vito Azzollini

Reputation: 23

Setting up Google Drive API in Python

I have been trying to set up a very simple python program to connect to Google Drive API, I have tried dozens of different approaches I found online but none seem to be working, documentation is all over the place and I cant get it to work.

I am in need of an approach that does not prompt the user to grant access seeing that I am going to be accessing my own personal drive, I would like it to do it automatically without me having to accept every time.

Could anyone send me a complete (very simplistic) working code template that I can use to connect to googles drive API using python?

This is my latest attempt, you might modify this one or create a new one, I just need it to work :(

import google.oauth2.credentials
import google_auth_oauthlib.flow
from oauth2client.client import OAuth2WebServerFlow, FlowExchangeError

# Use the client_secret.json file to identify the application requesting
# authorization. The client ID (from that file) and access scopes are required.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    'client_secret.json',
    scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'])

# Indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required.
flow.redirect_uri = 'http://localhost:8888/'

# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
    # Enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type='offline',
    # Enable incremental authorization. Recommended as a best practice.
    include_granted_scopes='true')

print(state)

# code = input('Enter verification code: ').strip()

try:
    credentials = flow.step2_exchange(state)
    print(json.dumps(json.loads(credentials._to_json([])), sort_keys=True, indent=4))
except FlowExchangeError:
    print("Your verification code is incorrect or something else is broken.")
    exit(1)

Bonus: I am going to use this to upload a CSV file and then edit the same file with new data

Thanks alot for all the help.

Upvotes: 1

Views: 2113

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116869

You should be using a service account. Service accounts are like dummy users. A service account has its own drive account which you can access programticlly and upload and download to and from. You can also share a folder or folders on your own google drive account with the server account. There by per-authorizing it and giving it permissions to access your drive account. There will be no consent screen or login screen displayed

The main difference is the login methods. I dont see a python service account drive example but there is one for google analytics api. If you take a look at that it shouldn't be to hard to alter it. You will need to make service account credentials the credentials file you have now will not work.

Hello Analytics API: Python quickstart for service accounts

"""A simple example of how to access the Google Analytics API."""

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


def get_service(api_name, api_version, scopes, key_file_location):
    """Get a service that communicates to a Google API.

    Args:
        api_name: The name of the api to connect to.
        api_version: The api version to connect to.
        scopes: A list auth scopes to authorize for the application.
        key_file_location: The path to a valid service account JSON key file.

    Returns:
        A service that is connected to the specified API.
    """

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
            key_file_location, scopes=scopes)

    # Build the service object.
    service = build(api_name, api_version, credentials=credentials)

    return service


def get_first_profile_id(service):
    # Use the Analytics service object to get the first profile id.

    # Get a list of all Google Analytics accounts for this user
    accounts = service.management().accounts().list().execute()

    if accounts.get('items'):
        # Get the first Google Analytics account.
        account = accounts.get('items')[0].get('id')

        # Get a list of all the properties for the first account.
        properties = service.management().webproperties().list(
                accountId=account).execute()

        if properties.get('items'):
            # Get the first property id.
            property = properties.get('items')[0].get('id')

            # Get a list of all views (profiles) for the first property.
            profiles = service.management().profiles().list(
                    accountId=account,
                    webPropertyId=property).execute()

            if profiles.get('items'):
                # return the first view (profile) id.
                return profiles.get('items')[0].get('id')

    return None


def get_results(service, profile_id):
    # Use the Analytics Service Object to query the Core Reporting API
    # for the number of sessions within the past seven days.
    return service.data().ga().get(
            ids='ga:' + profile_id,
            start_date='7daysAgo',
            end_date='today',
            metrics='ga:sessions').execute()


def print_results(results):
    # Print data nicely for the user.
    if results:
        print 'View (Profile):', results.get('profileInfo').get('profileName')
        print 'Total Sessions:', results.get('rows')[0][0]

    else:
        print 'No results found'


def main():
    # Define the auth scopes to request.
    scope = 'https://www.googleapis.com/auth/analytics.readonly'
    key_file_location = '<REPLACE_WITH_JSON_FILE>'

    # Authenticate and construct service.
    service = get_service(
            api_name='analytics',
            api_version='v3',
            scopes=[scope],
            key_file_location=key_file_location)

    profile_id = get_first_profile_id(service)
    print_results(get_results(service, profile_id))


if __name__ == '__main__':
    main()

The main thing you will need to change in the code is the scope api name and version. Once you have a drive service created the code will be the same as the Oauth2 format.

Upvotes: 1

Related Questions