簡體   English   中英

使用訪問令牌的gmail api禁止錯誤

[英]gmail api forbidden error with using the access token

我正在嘗試使用gmail api從中獲取數據。 以下是程序工作流程。 首先,我向Google服務器提交請求,以便用戶可以給我的應用指定權限:

def oauth_connect(request):
    if request.method == 'GET' and request.user.is_authenticated():
        auth_uri = 'https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/gmail.compose&client_id={0}&redirect_uri={1}&approval_prompt=force'.format(GOOGLE_CLIENT_ID, HOSTNAME_URI + 'oauth2callback')
        return HttpResponseRedirect(auth_uri)

在回調中,我堅持使用Google發送的訪問令牌。 我還期望刷新令牌會在響應中,但顯然它不存在。

def google_oauth_return(request):
    if request.method == 'GET' and request.user.is_authenticated():
        # Get the auth code from the GET request
        code = request.GET['code']
        agent = Agent.objects.get(user=request.user)

        # Create a list of 2-tuples for the parameters to send in the POST request back to eventbrite to get the auth token
        post_data = [('code', code), ('client_secret', GOOGLE_CLIENT_SECRET), ('client_id', GOOGLE_CLIENT_ID), 
            ('redirect_uri', HOSTNAME_URI + 'oauth2callback'), ('grant_type', 'authorization_code'),
        ]

        # Send POST request and get json sent back
        result = urllib2.urlopen('https://accounts.google.com/o/oauth2/token', urllib.urlencode(post_data))
        pprint.pprint(result)
        # Load the json into a python dict
        data = json.load(result)
        pprint.pprint(data)
        # Get the access token
        access_token = data['access_token']
        expiration_time = data['expires_in']
        if 'refresh_token' in data:
            refresh_token = data['refresh_token']
        else:
            refresh_token = ''
        agent.google_access_token = access_token
        agent.google_access_token_expiration_time = datetime.utcnow().replace(tzinfo=tz.tzutc()) + timedelta(seconds=int(expiration_time))
        agent.google_refresh_token = refresh_token
        agent.save()

        return HttpResponseRedirect('/profile')

但是,當我使用訪問令牌提取數據時,我遇到了HTTP 403禁止的錯誤。

def get_gmail_data(request):
    if request.method == 'GET':
        agent = Agent.objects.get(user=request.user)
        access_token = agent.google_access_token
        pprint.pprint(access_token)
        expiration_time = agent.google_access_token_expiration_time
        # TODO Check if access token expired. tzinfo=tz.utc is used because utcnow returns a offset-naive datetime
        # Set up HTTPS request and add access token as a header
        get_mail = urllib2.Request('https://www.googleapis.com/gmail/v1/users/me/messages')
        get_mail.add_header('Authorization', 'Bearer ' + access_token)
        # Open the connection and get the json, turn it into a python dict
        gmail_data = urllib2.urlopen(get_mail)
        gmail = json.load(gmail_data)

        pprint.pprint(gmail)

我是oauth的新手,所以如果有人可以指出問題,這將是有幫助的。

根據Google APIs Explorer ,您使用的OAtuth 2.0范圍是:

https://www.googleapis.com/auth/gmail.compose

…僅允許:

管理草稿並發送電子郵件

但是您正在嘗試將其用於此:

https://www.googleapis.com/gmail/v1/users/me/messages

即查看電子郵件。 您需要正確的范圍:

https://mail.google.com/

… 這使得:

查看和管理您的郵件

因此錯誤消息是完全適當的。 您已請求並獲得執行某項操作的權限,嘗試執行其他操作,並獲得權限錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM