通过以下步骤,我可以以编程方式在Google工作表中创建电子表格,但工作表的所有者是开发者帐户(以“gserviceaccount.com”结尾的疯狂字符串),我的普通帐户无法查看电子表格。为了向Google用户添加读/写权限,还需要做些什么?如何更改Google表格电子表格的所有者?
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery
# ... json_key is the json blob that has the credentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_dict(json_key, scope)
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet = {
"properties": {"title": "my test spreadsheet"}
}
service.spreadsheets().create(body=spreadsheet).execute()
编辑:
我试图改变范围['https://www.googleapis.com/auth/drive']
但低于仍然回答不为我工作。当我运行
print [xx for xx in dir(service) if not xx.startswith('_')]
我得到
['new_batch_http_request', u'spreadsheets']
换句话说,permissions()
不是service
的方法,因为我已经定义的服务。我应该做什么不同?
嗯,你的'服务'和我的一样吗?我得到这个: 'AttributeError:'Resource'对象没有属性'permissions'' 上的''return service.permissions()。insert' ...“位。换句话说,当我在键入“'service.'”后,在IPython中选中标签时,''permissions()'“似乎不是成员方法。 – dslack
哦,也许问题是我的'范围'? – dslack
我在上面的帖子中添加了一个编辑,回复了您的建议,因为我认为阅读后的格式比评论格式更容易。 Upshot,'范围'似乎不是问题。(?) – dslack