javamonkey79
javamonkey79

Reputation: 17775

oauth google sheets python sharing failure

I have the following code:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
from httplib2 import Http

SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scopes=SCOPES)
http_auth = credentials.authorize(Http())

service = discovery.build('sheets', 'v4', credentials=(credentials))

spreadsheet_body = {
    'properties': {'title': 'Test Sheet'},
    "sheets": [
        {
            "data": [
                {
                    "startRow": 0,
                    "startColumn": 0,
                    "rowData": [
                        {
                            "values": [
                                {
                                    "userEnteredValue": {
                                        "stringValue": "foo"
                                    },
                                },
                                {
                                    "userEnteredValue": {
                                        "stringValue": "bar"
                                    },
                                },
                            ],
                        },
                        {
                            "values": [
                                {
                                    "userEnteredValue": {
                                        "stringValue": "row2"
                                    },
                                },
                                {
                                    "userEnteredValue": {
                                        "stringValue": "row2, col2"
                                    },
                                },
                                {
                                    "userEnteredValue": {
                                        "formulaValue": "=HYPERLINK(\"https://www.foo.com\")"
                                    },
                                },
                            ],
                        },
                    ],
                }
            ],
        }
    ],
}

request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()

print response

This works, and the sheet is created with a URL, but, when I try to open the URL, I get:

google auth error

My account has all permissions on the service account, so, I don't think it is a service account setup issue, but, I could be wrong.

Upvotes: 1

Views: 58

Answers (1)

javamonkey79
javamonkey79

Reputation: 17775

While I did not find a direct fix for this, I did find a workaround via the gspread library:

gc = gspread.authorize(credentials)
sht = gc.open_by_key(response['spreadsheetId'])
sht.share('edmunds.com', perm_type='domain', role='writer')

Upvotes: 1

Related Questions