在Python中更正Gmail App的范围

时间:2015-03-10 01:35:01

标签: python python-2.7 gmail gmail-api

当我运行快速入门流程第3步的代码时,我总是留下错误:无效范围'信息。我尝试了他们提供的几个不同的网站变体,无法弄清楚如何进入下一步和最后一步。

https://developers.google.com/gmail/api/quickstart/quickstart-python

我做了一些改动,以达到我已经到过的地步。我改变了:

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  import argparse
from oauth2client import tools
argparser = argparse.ArgumentParser(
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[tools.argparser])
flags = argparser.parse_args(sys.argv[1:])
credentials = tools.run_flow(flow, Storage, flags)

我坚持的部分是......

# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'

因此,截至目前我的代码是:

#!/usr/bin/python

import httplib2
import sys

from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

# From samples/analytics/sample_utils.py in the google-api-python-client source

def process_flags(argv):
  """Uses the command-line flags to set the logging level.

  Args:
    argv: List of command line arguments passed to the python script.
  """

  # Let the gflags module process the command-line arguments.
  try:
    argv = FLAGS(argv)
  except gflags.FlagsError, e:
    print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
    sys.exit(1)

  # Set the logging according to the command-line flag.
  logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))

# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'

# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/o/oauth2/gmail'

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  import argparse
from oauth2client import tools
argparser = argparse.ArgumentParser(
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[tools.argparser])
flags = argparser.parse_args(sys.argv[1:])
credentials = tools.run_flow(flow, Storage, flags)

# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)

# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)

# Retrieve a page of threads
threads = gmail_service.users().threads().list(userId='me').execute()

# Print ID for each thread
if threads['threads']:
  for thread in threads['threads']:
    print 'Thread ID: %s' % (thread['id'])

2 个答案:

答案 0 :(得分:0)

您已设置以下范围:

https://www.googleapis.com/o/oauth2/gmail

不是valid scope

答案 1 :(得分:0)

上面提出问题的方式,请参阅@ alfasin的回答。

但是,根据您对@ alfasin关于重定向URI的回答的评论以及您使用Gmail-API快速入门代码的事实,您的问题可能如下:

在Google Developers Console中设置新项目时,为OAuth设置的默认重定向URI为https://www.example.com。如果您从未更改过,则需要在开发人员控制台中更改它并下载新的凭据JSON文件。确保更改应用程序以使用新文件。

(您需要将重定向URI设置为您的应用程序尝试重定向到的任何URI,如果这确实发生在您身上,可能会在错误页面上显示,可能是http://localhost:8090/中的{{1}}情况)。