Here's how to create the Google Spreadsheet itself.
The following articles were helpful for setting up OAuth and accessing spreadsheets. Accessing spreadsheets from Python using OAuth 2.0
Also, the following was helpful for creating Google Spreadsheet itself. docs_v3_example.py Create new spreadsheet (Google API / Python)
We have confirmed the operation in the environment of Python 2.7.10 and gdata 2.0.18.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from oauth2client.service_account import ServiceAccountCredentials
import gdata.docs.client
# name definitions
APP_NAME = 'GDataDocumentsAPISample'
name_spr = 'Sample Spreadsheet'
# resources for credential
json_key = 'gspread-test.json'
scope = ['https://docs.google.com/feeds']
# create goole data docs client
client = gdata.docs.client.DocsClient(source=APP_NAME)
client.http_client.debug = True
#client.http_client.debug = False
# create credentials
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)
auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
# authorise
auth_token.authorize(client)
# create document as spreadsheet
document = gdata.docs.data.Resource(type='spreadsheet', title=name_spr)
document = client.CreateResource(document)
# add ACL to spreadsheet
acl_entry = gdata.docs.data.AclEntry(
scope=gdata.acl.data.AclScope(value='[email protected]', type='user'),
role=gdata.acl.data.AclRole(value='writer'),
)
client.AddAclEntry(document, acl_entry, send_notifications=False)
print('Created:', document.title.text, document.resource_id.text)
As a little addicted ... The account when executing Oauth is different from your own Google account, [email protected]
is the owner, and it is private by default, so you have to set acl_entry on Google Drive Spreadsheet cannot be displayed with.
Recommended Posts