2012-06-05 2 views
7

Username-Passwordフロー(this記事の最後のセクションで説明したように)を使用して認証トークンを取得しようとしています。salesforce.comでgrant_type = passwordのoauthフローを使用するにはどうすればよいですか?

私が(関連性の場合は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

あなたはあなたのコードおよび/または実際のHTTP要求のキャプチャを投稿することができます。 – superfell

答えて

19

通常、コンテンツタイプのヘッダーが正しい値に設定されていないため、これはapplication/x-www-form-urlencodedである必要があります。

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

+0

迅速な対応に感謝します。私はこの記事の中で、私が従っていた記事の中でその要求について何の言及も見つけられませんでした。 – Symmetric

6

JAVAにsalesforce.comとgrant_type =パスワードの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())); 
} 
+0

HI Chirag、上記のコードスニペットを書いてみましたが、まだ私は問題の下になっていますログイン応答:{ "error": "invalid_grant " " error_description ":" authentication failure " }正しい資格情報を使用しましたが、私が持っていても、あなたがこれを指導できる場合は教えてください。 – Ashish

0

あなたはフォームデータの「パスワード」としてgrant_typeを設定する必要があります。下記参照。

フォームデータを広告 grant_typeを渡す必要があります=パスワード&ユーザ名= nilavghosh%40gmail.com &パスワード= ******

関連する問題