2017-10-05 55 views
1

我有以下代碼:OAuth的谷歌張蟒蛇共享失敗

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,我得到:

google auth error

我的帳戶擁有服務帳戶的所有權限,所以我不認爲這是服務帳戶設置問題,但是,我可能是錯的。

回答

1

雖然我沒有找到這樣的直接修復,我發現通過gspread庫解決方法:

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

我已經上傳了類似的問題,但我不使用的域。如何有人默認它? –