我正在尝试使用Username-Password流程获取授权令牌(如this文章最后一节所述)。
我发送以下请求(使用Python的httplib,如果相关的话):
https://login.salesforce.com/services/oauth2/token
POST data:
username=<un>&client_secret=<consumer_secret>&password=<pw+token>&grant_type=password&client_id=<consumer_key>
得到答复:
400 Bad Request
{"error":"unsupported_grant_type","error_description":"grant type not supported"}
密码grant_type真的不受支持,或者我错过了什么?即使我发送肯定有效的grant_type(例如authorization_code),它似乎也会出现此错误。
请注意,我已经尝试了答案here中的建议,但它们对我不起作用。
答案 0 :(得分:20)
通常这是因为内容类型标头尚未设置为正确的值,应为application/x-www-form-urlencoded
。
还要确保您的参数已正确编码(特别是如果您手动构建POST有效内容)。
答案 1 :(得分:6)
以下是有关如何在JAVA中使用grantforce = grant_type = password oauth flow的详细功能/逻辑:
// Authenticate via OAuth
JSONObject response = oauthLogin();
System.out.println("Login response: " + response.toString(2));
if (!response.has("access_token")) {
throw new Exception("OAuth failed: " + response.toString());
}
..........................
private static JSONObject oauthLogin() throws Exception {
org.eclipse.jetty.client.HttpClient jettyHttpClient = new org.eclipse.jetty.client.HttpClient();
jettyHttpClient.start();
String url = LOGIN_SERVER + "/services/oauth2/token";
ContentExchange exchange = new ContentExchange();
exchange.setMethod("POST");
exchange.setURL(url);
String message = "grant_type=password&client_id=" + CLIENT_ID
+ "&client_secret=" + CLIENT_SECRET + "&username=" + USERNAME
+ "&password=" + PASSWORD;
exchange.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
exchange.setRequestContentSource(new ByteArrayInputStream(message
.getBytes("UTF-8")));
jettyHttpClient.send(exchange);
exchange.waitForDone();
return new JSONObject(new JSONTokener(exchange.getResponseContent()));
}
答案 2 :(得分:0)
您必须将grant_type设置为&#34;密码&#34;在表格数据中。见下文。
表单数据应该通过广告 grant_type =密码&安培;用户名= nilavghosh%40gmail.com&安培;密码= ******