首页 > 解决方案 > GMAIL API:new_request() 最多接受 1 个位置参数(给定 2 个)

问题描述

我正在使用 gmail api。我收到以下警告:

new_request() 最多接受 1 个位置参数(给定 2 个)

从此代码:

message = GMAIL.users().messages().get(userId=user_id, id=m_id).execute() # fetch the message using API

这是我的完整代码(主要取自 gmail API):

"""
Shows basic usage of the Gmail API.

Lists the user's Gmail labels.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow, argparser
from oauth2client import tools

from colorama import Fore, Back, Style
from colorama import init

# use Colorama to make Termcolor work on Windows too
init()

# Setup the Gmail API

def get_credentials():
    store = file.Storage('credentials.json')
    creds = store.get()
    flow = OAuth2WebServerFlow(
        client_id='845-ehe.apps.googleusercontent.com',
        client_secret='curo',
        scope = 'https://www.googleapis.com/auth/gmail.modify',
        user_agent='people_cal2sms')

    if not creds or creds.invalid:

        flags = tools.argparser.parse_args(args=[])
        creds = tools.run_flow(flow, store, flags)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

# Importing required libraries
from apiclient import discovery

# Creating a storage.JSON file with authentication details
SCOPES = 'https://www.googleapis.com/auth/gmail.modify' # we are using modify and not readonly, as we will be marking the messages Read
store = file.Storage('storage.json') 
creds = store.get()
if not creds or creds.invalid:
    flags = tools.argparser.parse_args(args=[])
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags)
GMAIL = discovery.build('gmail', 'v1', http=creds.authorize(Http()))

user_id =  'me'
label_id_one = 'INBOX'

# Getting all the unread messages from Inbox
# labelIds can be changed accordingly
unread_msgs = GMAIL.users().messages().list(userId='me',labelIds=[label_id_one], q="label:clinic_sms after:2018/9/14").execute()

# We get a dictonary. Now reading values for the key 'messages'
mssg_list = unread_msgs.get('messages', [])

print ("Total unread messages in inbox: ", str(len(mssg_list)))

final_message_dict_list = [ ]

def retrieve_phone_2_gmail_message_dict():
    phone_2_gmail_message_dict = {}
    for mssg in mssg_list:
        message_dict = { }
        m_id = mssg['id'] # get id of individual message
        message = GMAIL.users().messages().get(userId=user_id, id=m_id).execute() # fetch the message using API

标签: pythonpython-3.xgmail-api

解决方案


推荐阅读