Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自Gmail API请求的Excel文件_C#_Http_Encoding_Gmail Api - Fatal编程技术网

C# 来自Gmail API请求的Excel文件

C# 来自Gmail API请求的Excel文件,c#,http,encoding,gmail-api,C#,Http,Encoding,Gmail Api,我已经创建了一个程序,可以获取电子邮件中文件的附件ID,然后使用Gmail获取附件。我感兴趣的文件类型是Excel文件,所以假设我得到的附件是.xlsx 根据API参考,我需要的响应字段是数据。我可以用C#获取此字段,但我无法将其转换为excel文件 在Python或C#中,任何示例都会非常有用。我知道您在Gmail中有一封附有XLSX文件的邮件,您希望将其下载到本地文件夹中。我假设您已经知道您的和标识符,如果不是这样,请原谅我并写一条评论,这样我可以进一步帮助您。如果您已经有标识符,此代码将帮

我已经创建了一个程序,可以获取电子邮件中文件的附件ID,然后使用Gmail获取附件。我感兴趣的文件类型是Excel文件,所以假设我得到的附件是.xlsx

根据API参考,我需要的响应字段是数据。我可以用C#获取此字段,但我无法将其转换为excel文件


在Python或C#中,任何示例都会非常有用。

我知道您在Gmail中有一封附有XLSX文件的邮件,您希望将其下载到本地文件夹中。我假设您已经知道您的和标识符,如果不是这样,请原谅我并写一条评论,这样我可以进一步帮助您。如果您已经有标识符,此代码将帮助您:

#!/usr/bin/env python3

# IMPORTs for Gmail API
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# IMPORTs for data manipulation
from base64 import urlsafe_b64decode
import email

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


def main():
    # Gmail API configuration
    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    service = build('gmail', 'v1', credentials=creds)

    messageID = "{MESSAGE IDENTIFIER}"
    attachmentID = "{ATTACHMENT IDENTIFIER}"

    # Step I - Getting the attachment
    response = service.users().messages().attachments().get(
        userId='me', messageId=messageID, id=attachmentID).execute()

    # Step II - Manipulating the data
    bytesFile = urlsafe_b64decode(response["data"])
    if bytesFile[0:2] != b'PK':
        raise ValueError('The attachment is not a XLSX file!')
    message = email.message_from_bytes(bytesFile)

    # Step III - Storing the file
    open('attachment.xlsx', 'wb').write(message.get_payload(decode=True))


if __name__ == '__main__':
    main()

初始设置是配置Gmail服务;这部分是我从电视上得到的。第一步是调用该方法以接收带有附件的JSON响应。文件将在属性
数据中接收。之后,在第二步中,代码将用于将
数据
转换为bytes对象。在继续之前,脚本将检查bytes对象的大小,以验证它确实是XLSX文件;如果不是,则会引发错误。最后,如果一切正常,将使用从字节对象创建消息对象

在第三个也是最后一个步骤中,脚本将使用以下方法将消息保存为
attachment.xlsx
。请注意,我是如何将
decode
参数用于
8bit
的头的。该文件将写入本地文件夹。如果你需要我澄清一些事情,请不要犹豫问我任何问题