How can I use the grant_type=password oauth flow with salesforce.com?
Asked Answered
S

3

9

I'm trying to get an authorization token using the Username-Password flow (as described in the final section of this article).

I'm sending the following request (using Python's httplib, in case that's relevant):

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>

And getting the response:

400 Bad Request
{"error":"unsupported_grant_type","error_description":"grant type not supported"}

Is the password grant_type really unsupported, or am I missing something? It seems to give this error even when I'm sending a grant_type that definitely does work (such as authorization_code).

Note that I've tried the suggestions in the answer here, and they don't work for me.

Soloist answered 5/6, 2012 at 1:27 Comment(1)
can you post your code and/or a capture of the actual http request.Glauce
G
21

Typically this is because the content-type header has not been set to the correct value, it should be application/x-www-form-urlencoded.

Also make sure your parameters are correctly encoded (especially if you're building the POST payload by hand).

Glauce answered 5/6, 2012 at 1:49 Comment(1)
Thanks for the quick response. I couldn't find any mention of that requirement in the article that I was following...Soloist
C
6

Below is detailed function/logic on how to use grant_type=password oauth flow with salesforce.com in JAVA:

    // 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()));
}
Cavazos answered 27/9, 2013 at 20:24 Comment(1)
HI Chirag, I tried as you written code snippet above , Still I am getting below issue Login response: { "error": "invalid_grant", "error_description": "authentication failure" } I have used right credential, even though I got , please let me know , if you can guide on this.Afore
A
0

You must set the grant_type as "password" in the form data. See below.

The form data should be passed ad grant_type=password&username=nilavghosh%40gmail.com&password=******

Ardra answered 22/12, 2015 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.