【问题标题】:Trying to understand OAuth2 refresh_token flow - keep getting invalid_grant试图了解 OAuth2 refresh_token 流程 - 不断获得 invalid_grant
【发布时间】:2013-12-25 12:24:28
【问题描述】:

我对refresh_token流程(http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com)的理解如下:

  1. 获取初始令牌
  2. 使用 (1) 中的令牌定期执行“refresh_token”

当我尝试代表用户使用“密码”授权获取初始令牌时,随后的“刷新令牌”失败。我做错了什么?

考虑下面的python示例:

#!/usr/bin/env python

import requests
import sys
from optparse import OptionParser
import json

usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.description = """Get a login token from salesforce
"""

parser.add_option("-u", "--username", dest="username", help="User name")
parser.add_option("-p", "--password", dest="password", help="User password")
parser.add_option("-t", "--securityToken", dest="token", help="User's security token")
parser.add_option("-i", "--client_id", dest="client_id", help="OAuth client_id (aka SF Consumer Id)")
parser.add_option("-s", "--client_secret", dest="client_secret", help="Client Secret  (aka SF Consumer Secret)")

(options, args) = parser.parse_args()

resp = requests.post('https://login.salesforce.com/services/oauth2/token', params={
   "grant_type":"password",
   "client_id":options.client_id,
   "client_secret":options.client_secret,
   "username":options.username,
   "password":options.password + options.token,
   "redirect_url":"https://localhost:8080/ls/api/oauth"})

accessInfo = json.loads(resp.text)
access_token = accessInfo["access_token"]
print "Initial Token:", json.dumps(accessInfo, indent=4)

resp = requests.post('https://login.salesforce.com/services/oauth2/token', params={
   "grant_type":"refresh_token",
   "client_id":options.client_id,
   "client_secret":options.client_secret,
   "refresh_token":access_token,
   "redirect_url":"https://localhost:8080/ls/api/oauth"})

refreshInfo = json.loads(resp.text)

print "Refresh token:", json.dumps(refreshInfo, indent=4)

【问题讨论】:

    标签: python oauth-2.0 salesforce force.com


    【解决方案1】:

    您不会通过用户名/密码流程获得刷新令牌,因为 (a) 您拥有用户的密码,并且可以在需要时获得新的访问令牌,并且 (b) 无法获得用户的授权,这基本上就是刷新令牌所代表的。

    【讨论】:

    • 显然这是真的,但这让我想知道:为什么?为什么在 OAuth 2 支持刷新令牌时需要第三方存储用户密码?
    • 刷新令牌代表用户对该应用程序访问具有给定范围的 API 的授权。有了用户名/密码,用户就脱离了循环,所以授权不一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2012-09-25
    • 2020-02-07
    • 2015-10-23
    • 2012-04-19
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多