4
votes

I followed the Google Sheet Python API Quickstart guide (https://developers.google.com/sheets/api/quickstart/python) and was able to get it working using their supplied code:

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """

    # If modifying these scopes, delete your previously saved credentials
    # at ~/.credentials/sheets.googleapis.com-python-quickstart.json
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    CLIENT_SECRET_FILE = 'my/path/client_secret.json'
    APPLICATION_NAME = 'Google Sheets API Python Quickstart'

    credential_path = 'my/path/sheets.googleapis.com-python-quickstart.json'

    store = Storage(credential_path)
    credentials = store.get()

    ## !!!!! Is this needed?
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)

    return credentials

In the default setup I downloaded two JSON files:

  • client_secret.JSON
    • downloaded to project directory.
  • sheets.googleapis.com-python-quickstart.JSON
    • downloaded to ~/.credentials directory

The sheets.googleapis.com JSON file starts with:

"_module": "oauth2client.client".

Question 1: What is the purpose for each of these JSON files?

Question 2: Are both of these JSON files needed to successfully use the Google Sheets API?

  • I am thinking no, as I am able to get the API working without the client_secret.JSON file.
1

1 Answers

3
votes

How about this answer? I think when you know the OAuth2 process for retrieving access token and refresh token, you can understand the meaning of both files. The flow for retrieving access token and refresh token using OAuth2 is as follows.

Flow :

  1. Download client_secret.JSON from the API Console.
    • client_secret.JSON includes client_id, client_secret and redirect_uris.
  2. Retrieve an authorization code using scopes and client_id from client_secret.JSON.
  3. Retrieve access token and refresh token using the authorization code, client_id, client_secret and redirect_uris.
    • Retrieved access token, refresh token and other parameters are saved to the file of sheets.googleapis.com-python-quickstart.JSON.

Note :

  • When you run the Quickstart for the first time, the authorization process using your browser is launched. At that time, the script of Quickstart retrieves the authorization code using client_id and scopes, and then the access token and refresh token are retrieved using the authorization code, client_id, client_secret and redirect_uris.
  • After the first run of the Quickstart, the access token is retrieved by the refresh token from sheets.googleapis.com-python-quickstart.JSON. By this, retrieving the authorization code using browser is not required to do. So when there is sheets.googleapis.com-python-quickstart.JSON, client_secret.JSON is not required.
    • I think that this leads to an answer for your Question 2.
  • But, if you want to change scopes and/or credentials of client_secret.JSON, the authorization process using browser and retrieving the authorization code are required to do. For this, you have to remove sheets.googleapis.com-python-quickstart.JSON and authorize again. At that time, at Quickstart, client_secret.JSON is used again.

References :

If this is not useful for you, I'm sorry.