9

ユーザー名-パスワードフローを使用して認証トークンを取得しようとしています(この記事の最後のセクションで説明されています)。

私は次のリクエストを送信しています(関連する場合は、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など)を送信している場合でも、このエラーが発生するようです。

私はここの答えの提案を試しましたが、それらは私にはうまくいきません。

4

3 に答える 3

21

通常、これは content-type ヘッダーが正しい値に設定されていないためapplication/x-www-form-urlencodedです。

また、パラメータが正しくエンコードされていることを確認してください (特に、手動で POST ペイロードを作成している場合)。

于 2012-06-05T01:49:58.867 に答える
6

以下は、JAVA の salesforce.com で grant_type=password oauth フローを使用する方法に関する詳細な関数/ロジックです。

    // 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()));
}
于 2013-09-27T20:24:42.247 に答える