gpt4 book ai didi

python - 试图了解 OAuth2 refresh_token 流程 - 不断获得 invalid_grant

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:00 24 4
gpt4 key购买 nike

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

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

当我尝试代表用户使用“密码”授权获取初始 token 时,随后的“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)

最佳答案

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

关于python - 试图了解 OAuth2 refresh_token 流程 - 不断获得 invalid_grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com