Google Drive API服务帐户身份验证,使用Python上载图像文件并将其插入复制的文档中

Google Drive API服务帐户身份验证,使用Python上载图像文件并将其插入复制的文档中,python,google-api,google-drive-api,google-api-python-client,Python,Google Api,Google Drive Api,Google Api Python Client,Google Drive API文档在帮助确定使用服务帐户进行身份验证的最佳方式方面并不是非常出色,我可以将.png文件上传到驱动器。我的最终目标是上传一个.png文件,复制一个模板文档,使用文本替换批量更新该文档,并将新上传的.png图像插入该文档 示例代码如下: from dotenv import load_dotenv from google.oauth2 import service_account from googleapiclient.discovery import build

Google Drive API文档在帮助确定使用服务帐户进行身份验证的最佳方式方面并不是非常出色,我可以将.png文件上传到驱动器。我的最终目标是上传一个.png文件,复制一个模板文档,使用文本替换批量更新该文档,并将新上传的.png图像插入该文档

示例代码如下:

from dotenv import load_dotenv
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

def credentials_from_file():
  credentials = service_account.Credentials.from_service_account_file(
    os.getenv('SERVICE_ACCOUNT_FILE'), 
    scopes=os.getenv('SCOPES')
  )
  drive_service = build('drive', 'v3', credentials=credentials)
  return drive_service

def google_upload(drive_service, metadata_name, parents, permissions, file_path, mime_type):
  file_metadata = {'kind':'drive#file', 'name':metadata_name, 'parents':parents, 'permissions':permissions}
  media = MediaFileUpload(file_path, mimetype=mime_type)
  file = drive_service.files().create(body=file_metadata, media_body=media, fields='id', supportsAllDrives=True).execute()
  print('File ID: %s' % file.get('id'))
代码的实现

credentials = credentials_from_file()
drive_service = build('drive', 'v3', credentials=credentials)

metadata_name = custom_variables_png_table_img
parents = ['xxxx']
permissions = [{'kind':'drive#permission', 'emailAddress':os.getenv('EMAIL_ACCOUNT'), 'role':'owner'}]
file_path = custom_variables_png_table_img
mime_type = 'image/png'
google_upload(drive_service, metadata_name, parents, permissions, file_path, mime_type)
编辑: 看来我忘记写了,问题是。它是双重的

  • 在尝试运行
    google\u upload()
    函数时,我一直遇到两个错误,这看起来像是服务帐户的身份验证错误
  • 错误#1:
    jwt_grant access_token=response_data[“access_token”]keyror:“access_token”

    上述异常是以下异常的直接原因:

    错误#2:
    google.auth.exceptions.RefreshError:('No access token in response',{'id\u token':'xxx'})

  • 正在对最近上载的图像文件正确设置权限

  • 您当前使用的代码与我以前看到的代码相同

    from apiclient.discovery import build
    from oauth2client.service_account import ServiceAccountCredentials
    
    
    SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
    KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE_PATH_TO_FILE>'
    
    
    
    def initialize_drive():
      """Initializes an Google Drive API V3 service object.
    
      Returns:
        An authorized Google Drive API V3 service object.
      """
      credentials = ServiceAccountCredentials.from_json_keyfile_name(
          KEY_FILE_LOCATION, SCOPES)
    
      # Build the service object.
      driveService = build('drive', 'v4', credentials=credentials)
    
      return driveService
    

    我不得不为我糟糕的英语水平道歉。我能问一下你当前问题的详细情况吗?直接问吧。你有什么问题?谢谢你的回答。我不得不再次为我糟糕的英语水平道歉。我无法从你的问题中理解你当前的问题。所以我问了它的细节。什么是正确的方式来验证服务帐户,以便能够使用API将.png图像上载到驱动器,然后为新文件设置正确的权限?谢谢您的回复。从你的回复来看,这个帖子对你的情况有用吗?感谢您的评论@DaImTo。我用我看到的两个错误/问题更新了上面的问题/问题。你在这里说的一切都有道理。我已经找到了用纯文本更新文档部分的方法,并且知道如何找到添加图像的方法,如果我能够正确获得图像的权限和访问权限的话。如果您能够,请查看原始问题的更新编辑以进一步澄清。回答1:介意给我们一份您在os中拥有的内容的打印件。getenv(“范围”)回答2:请记住,您不能在上载调用中设置文件的权限。上传文件后,需要设置文件的权限。(这是谷歌的一个功能)#1:SCOPES输出:['',#2:得到了这个结果,并适当地调整了代码。SCOPES=os.getenv('SCOPES')刚刚这样做了,得到了一个新错误:credentials=service\u account.credentials。from\u service\u account\u文件(os.getenv('service\u account\u文件'),os.getenv('SCOPES'))类型错误:from\u service\u account\u文件()接受2个位置参数,但给出了3个
     service = build('docs', 'v1', credentials=creds)