Coo*_*ova 3 http oauth salesforce oauth-2.0 google-oauth
从Salesforce中使用Google OAuth时,我收到400错误请求.以下错误与invalid_type无效有关,但如果您查看"使用刷新令牌"下的文档,您将看到它是正确的.
https://developers.google.com/identity/protocols/OAuth2WebServer
错误:
{
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: "
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用refresh_token交换访问令牌,并且可以使用CURL使用以下代码成功完成.
curl \
-d refresh_token=REFRESH_TOKEN \
-d client_id=CLIENT_ID \
-d client_secret=CLIENT_SECRET \
-d grant_type=refresh_token https://www.googleapis.com/oauth2/v4/token
Run Code Online (Sandbox Code Playgroud)
我在Salesforce中使用的代码:
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
req.setHeader('Content-Length', '0');
req.setHeader('client_id', 'CLIENT_ID');
req.setHeader('client_secret', 'CLIENT_SECRET');
req.setHeader('refresh_token', 'REFRESH_TOKEN');
req.setHeader('grant_type', 'refresh_token');
req.setEndpoint('https://www.googleapis.com/oauth2/v4/token');
req.setMethod('POST');
return http.send(req);
Run Code Online (Sandbox Code Playgroud)
我在 fiddler 中发现了同样的问题。我添加此评论是因为它可能对某人有帮助。
https://oauth2.googleapis.com/token
Content-Type: application/x-www-form-urlencoded
Request Body:
code=<some code here>&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https%3A//oauth2.example.com/code&
grant_type=authorization_code
Run Code Online (Sandbox Code Playgroud)
古尔API响应:
{"error": "unsupported_grant_type", "error_description": "Invalid grant_type: "}
Run Code Online (Sandbox Code Playgroud)
出现此问题的原因是请求正文的每个参数之间都有换行符。如果删除所有换行符,则将所有值保留在单行请求中即可正常工作。例如请求正文:
code=<some code here>&client_id=your_client_id&client_secret=your_client_secret&redirect_uri=https%3A//oauth2.example.com/code&grant_type=authorization_code
Run Code Online (Sandbox Code Playgroud)
的-d
卷曲选项使用请求正文发送数据application/x-www-form-urlencoded
是在发送的OAuth2那些参数的支持方式之一内容类型.
-d
将POST请求中的指定数据发送到HTTP服务器,就像用户填写HTML表单并按下提交按钮时浏览器一样.这将导致curl使用content-type application/x-www-form-urlencoded将数据传递到服务器.
在Salesforce代码中,您设置了正确的内容类型,但随后将OAuth2相关参数作为附加标头发送,而不是在请求正文中发送它们.
您需要更新代码以使用application/x-www-form-urlencoded
编码在请求正文中发送参数.
归档时间: |
|
查看次数: |
2945 次 |
最近记录: |