How to create Google Sheets using Service Account Credential in Python?

家住魔仙堡 提交于 2020-03-25 17:38:29

问题


I created Service Account Credentials here and got json key service.json.

Then I tried:

from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
credentials = service_account.Credentials.from_service_account_file(
        'service.json', scopes=SCOPES)

drive = build('drive', 'v3', credentials=credentials)
file_metadata = {
    'name': 'sampleName',
    'parents': ['#### folderId ###'],
    'mimeType': 'application/vnd.google-apps.spreadsheet',
}
res = drive.files().create(body=file_metadata).execute()
print(res)

With an error:

<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

I found that without an Auth header, I am anonymous, and the quota for anonymous use is zero. How can I set header or the reason for this error is something else?

All I want is to create a spreadsheet with python in my gdrive folder from any computer without a need to click somewhere to grant access.


回答1:


  • You want to create new Spreadsheet using Drive API v3 with the service account.
  • You want to create new Spreadsheet to the specific folder of Google Drive of your Google account.
  • You want to achieve this using googleapis with python.
  • You have already been able to use Drive API.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:

  • I think that in your script, the scope is required to be modified for creating the file with Drive API. In this case, how about using https://www.googleapis.com/auth/drive?
  • In order to create the new Spreadsheet to the specific folder of Google Drive of your Google account, at first, it is required to share the folder in your Google Drive with the email of the service account. By this, new Spreadsheet is created with the service account to the specific folder of your Google Drive. Please be careful this.

Modified script:

When your script is modified, please modify it as follows.

from googleapiclient.discovery import build  # Added
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/drive']  # Modified
credentials = service_account.Credentials.from_service_account_file('service.json', scopes=SCOPES)

drive = build('drive', 'v3', credentials=credentials)
file_metadata = {
    'name': 'sampleName',
    'parents': ['#### folderId ###'],
    'mimeType': 'application/vnd.google-apps.spreadsheet',
}
res = drive.files().create(body=file_metadata).execute()
print(res)
  • About #### folderId ###, please set the folder ID shared with the email of the service account.

Note:

  • If you want to use both Sheets API and Drive API, please use SCOPES = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"].
  • In this case, the new Spreadsheet is directly created to the folder in your Google Drive. So you can see it by the browser.
    • If new Spreadsheet is created to the folder in Drive of the service account, it is required to create a permission for your Google account. Please be careful this. In this case, I think that this thread is useful.

References:

  • Files: create
  • Google API file permissions using python

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/60704698/how-to-create-google-sheets-using-service-account-credential-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!