Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 262
Symmetric Avatar asked Jun 05 '12 01:06

Symmetric


People also ask

What is Grant_type in oauth2?

In OAuth 2.0, the term “grant type” refers to the way an application gets an access token. OAuth 2.0 defines several grant types, including the authorization code flow. OAuth 2.0 extensions can also define new grant types.

What is Grant_type in Salesforce?

grant_type. The OAuth 2.0 grant type that the connected app requests. The value must be password for this flow. client_id. The consumer key of the connected app.


2 Answers

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).

like image 72
superfell Avatar answered Nov 11 '22 05:11

superfell


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()));
}
like image 43
Chirag Mehta Avatar answered Nov 11 '22 05:11

Chirag Mehta