代码之家  ›  专栏  ›  技术社区  ›  user1767754

googledriveapi:用户没有授予应用程序错误

  •  2
  • user1767754  · 技术社区  · 6 年前

    我在跟踪 Quickstart https://developers.google.com/drive/api/v3/quickstart/python . 我已经通过页面启用了驱动API,加载了

    `The user has not granted the app ####### read access to the file`
    

    我需要做的不仅仅是把这个范围放在代码中,还是需要激活其他东西?

    SCOPES = 'https://www.googleapis.com/auth/drive.file'
    client.flow_from_clientsecrets('credentials.json', SCOPES)
    
    2 回复  |  直到 6 年前
        1
  •  9
  •   user1767754    6 年前

    一旦你通过 Quick-Start Tutorial

    SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
    

    因此,在列出文件并决定下载之后,它将不起作用,因为您需要再次生成令牌,因此更改作用域不会重新创建或提示您输入第一次运行时发生的“google授权”。

    要强制生成令牌,只需删除当前令牌或使用新文件从存储器中存储密钥:

    store = file.Storage('tokenWrite.json')
    
        2
  •  1
  •   ethanenglish    6 年前

    我也遇到了同样的错误。我授权了整个范围,然后检索文件,并使用io.基地类将数据流化为文件。注意,您需要先创建文件。

    from __future__ import print_function
    from googleapiclient.discovery import build
    import io
    from apiclient import http
    from google.oauth2 import service_account
    
    SCOPES = ['https://www.googleapis.com/auth/drive']
    
    SERVICE_ACCOUNT_FILE = 'credentials.json'
    FILE_ID = <file-id>
    
    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    
    service = build('drive', 'v3', credentials=credentials)
    
    def download_file(service, file_id, local_fd):
    
      request = service.files().get_media(fileId=file_id)
      media_request = http.MediaIoBaseDownload(local_fd, request)
    
      while True:    
        _, done = media_request.next_chunk()
    
        if done:
          print ('Download Complete')
          return
    
    file_io_base = open('file.csv','wb')
    
    download_file(service=service,file_id=FILE_ID,local_fd=file_io_base)
    

    希望有帮助。

        3
  •  0
  •   Mrityunjai elo80ka    4 年前

    此后,即使您在google api控制台中更改了作用域,并在代码中添加了以下作用域:-

    SCOPES = ['https://www.googleapis.com/auth/drive']
    

    它将不会工作,直到您删除和更早的范围的令牌。pickle.