我正在构建一个需要API访问Gmail和Google日历的Django应用。我正在关注Google提供的the tutorial。当我可以通过命令行获取正确的身份验证URI时,如下所示:
>>> from oauth2client.client import OAuth2WebServerFlow
>>> from integrations.google import settings
>>> flow = OAuth2WebServerFlow(
... client_id=settings.CLIENT_ID,
... client_secret=settings.CLIENT_SECRET,
... scope='https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/gmail.readonly',
... redirect_uri='http://www.myapp.com/oauth/google/',
... )
>>> url = flow.step1_get_authorize_url()
>>> url
'https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&redirect_uri=http%3A%2F%2Fwww.myapp.com%2Foauth%2Fgoogle%2F&response_type=code&client_id=123345789.apps.googleusercontent.com&access_type=offline'
此网址指向正确的网页(“MyApp希望:查看您的电子邮件,消息和设置......”等),因此oauth2client库似乎按预期工作。
当我编写包含Django的RedirectiView时,问题似乎浮出水面;网址格式错误,我收到了来自Google的404错误:
错误:invalid_scope
某些请求的范围无效。 {有效= [https://www.googleapis.com/auth/calendar, https://www.googleapis.com/auth/gmail.readonly],无效= []}
所以看起来某种方式会在'scope'变量中添加额外的空格,或者可能会添加一个额外的'scope'变量?我不确定为什么会出现这个错误,但当我通过RedirectView.get_redirect_uri调用它时,它再次只会弹出
以下是我的观点:
from django.views.generic import RedirectView
from oauth2client.client import OAuth2WebServerFlow
from integrations.google import settings
class GetGoogleAuthURIView(RedirectView):
def get_redirect_url(self):
flow = OAuth2WebServerFlow(
client_id=settings.CLIENT_ID,
client_secret=settings.CLIENT_SECRET,
scope='https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/gmail.readonly',
redirect_uri='http://www.perryapp.com/oauth/google/',
)
url = flow.step1_get_authorize_url()
return url
我做错了什么?