【问题标题】:How can I use the grant_type=password oauth flow with salesforce.com?如何在 salesforce.com 中使用 grant_type=password oauth 流程?
【发布时间】:2012-06-09 01:36:30
【问题描述】:

我正在尝试使用用户名密码流程获取授权令牌(如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 中的建议,但它们对我不起作用。

【问题讨论】:

  • 您能否发布您的代码和/或实际 http 请求的捕获。

标签: oauth passwords salesforce


【解决方案1】:

通常这是因为 content-type 标头没有设置为正确的值,它应该是application/x-www-form-urlencoded

还要确保您的参数编码正确(尤其是在您手动构建 POST 有效负载时)。

【讨论】:

  • 感谢您的快速回复。我在我关注的文章中找不到任何提及该要求的内容...
【解决方案2】:

以下是有关如何在 JAVA 中将 grant_type=password oauth flow 与 salesforce.com 一起使用的详细功能/逻辑:

    // 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()));
}

【讨论】:

  • HI Chirag,我试过你写上面的代码 sn-p ,但我仍然遇到以下问题登录响应:{“error”:“invalid_grant”,“error_description”:“authentication failure”}我已使用正确的凭据,即使我得到了,请告诉我,如果您可以指导。
【解决方案3】:

您必须在表单数据中将 grant_type 设置为“密码”。见下文。

表单数据应该通过ad grant_type=password&username=nilavghosh%40gmail.com&password=******

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 2016-04-22
    • 2016-11-14
    • 2020-12-04
    • 2014-12-05
    • 2017-07-27
    • 1970-01-01
    • 2018-06-07
    • 2019-01-23
    相关资源
    最近更新 更多