我有以下代码:
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
这样可行,并且工作表是使用URL创建的,但是,当我尝试打开URL时,我得到:
我的帐户拥有服务帐户的所有权限,因此,我不认为这是服务帐户设置问题,但是,我可能是错的。
答案 0 :(得分:1)
虽然我没有找到针对此的直接修复,但我确实通过gspread库找到了解决方法:
gc = gspread.authorize(credentials)
sht = gc.open_by_key(response['spreadsheetId'])
sht.share('edmunds.com', perm_type='domain', role='writer')