【问题标题】:invalid_grant error while getting access token via OAuth 2.0通过 OAuth 2.0 获取访问令牌时出现 invalid_grant 错误
【发布时间】:2015-09-04 11:46:39
【问题描述】:

有一个python应用使用Health Graph API

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, redirect
from main.settings import CLIENT_ID, CLIENT_SECRET, RUNKEEPER_LOGIN_URL, ACCESS_TOKEN_URL
import requests


def index(request):
    return render_to_response('index.html')

def login(request):
    code = request.GET['code']
    post_data = {'grant_type': 'authorization_code',
                 'code': code,
                 'client_id': CLIENT_ID,
                 'client_secret': CLIENT_SECRET,
                 'redirect_uri': 'http://127.0.0.1:8000/welcome/'}

    req = requests.post(ACCESS_TOKEN_URL, data=post_data)

我的登录按钮将用户重定向到 Health Graph 登录页面,让我的应用等待授权码。

<a href="https://runkeeper.com/apps/authorize?redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2Flogin&response_type=code&client_id=xxxxxxxxx">

当我尝试通过调用登录函数获取访问令牌时,我收到“无效授权”错误作为请求。我试图将“authorization_code”替换为另一个词,但出现了另一个“不支持的授权类型”错误。换句话说,令牌服务器拒绝我从身份验证服务器获得的代码。我做错了什么?

【问题讨论】:

    标签: python python-2.7 oauth oauth-2.0


    【解决方案1】:

    重定向到授权端点中使用的redirect_uri 与您在login 函数中使用的不同,该函数在令牌端点交换code。正如http://developer.runkeeper.com/healthgraph/getting-started 的文档(“将您的应用程序连接到用户的健康图表帐户”,第 3 条)规定,它应该完全匹配:

    redirect_uri:您在将用户发送到时提供的确切 URL 上面的授权端点

    【讨论】:

      猜你喜欢
      • 2015-05-30
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多