2012-06-05 29 views
7

我試圖使用用戶名 - 密碼流獲得授權令牌(如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使用grant_type =密碼的OAuth流使用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())); 
} 
+0

HI Chirag,我在上面編寫代碼片段時嘗試過,仍然遇到以下問題登錄回覆:{ 「error」:「invalid_grant 「, 」error_description「:」身份驗證失敗「 }我已經使用了正確的憑據,即使我有,請讓我知道,如果你可以指導。 – Ashish

0

必須設置grant_type作爲形式數據的「密碼」。見下文。

表單數據應該傳遞廣告 grant_type =密碼&用戶名= nilavghosh%40gmail.com &密碼= ******

相關問題