2016-01-06 112 views
2

我一直在努力將我的應用程序與LinkedIn整合在一起,其文檔位於here。我創建了我的LinkedIn應用是能夠成功地獲取授權碼,但我正在試圖獲得訪問令牌時,出現以下錯誤:鏈接無法檢索訪問令牌缺少所需的參數錯誤

{「ERROR_DESCRIPTION」:「缺少必需的參數,包括無效 參數值,參數不止一次:無法檢索訪問令牌:APPID或重定向URI不匹配授權碼或授權碼已過期」,‘錯誤’:‘INVALID_REQUEST’}

我已經驗證以下爲真:

  1. 重定向URI對於授權請求和訪問令牌請求是相同的
  2. 我在使用授權代碼的20秒內發出。
  3. 我包括 「內容類型:應用程序/ x-WWW的形式,進行了urlencoded」 的請求頭

有些事情我都沒有成功嘗試:

  1. 發佈與請求的參數作爲URL
  2. 發佈與參數的請求作爲主體
  3. 的一部分發送重定向URI作爲同時使用編碼和純文本
  4. 的一部分一個獲取請求,而不是一個帖子。

這裏是我當前的代碼:

linkedin_access_token_url = "https://www.linkedin.com/uas/oauth2/accessToken?"+ 
       "grant_type=authorization_code"+ 
       "&code="+ authCode 
       + "&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp 
       + "&client_id=" + linkedin_client_id 
       + "&client_secret=" + linkedin_client_secret; 
     HttpClient http = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(linkedin_access_token_url); 
     try { 
      httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
      HttpResponse response = http.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      System.out.println("status code " + 
      response.getStatusLine().getStatusCode()); 
      System.out.println("statusreason"+ 
      response.getStatusLine().getReasonPhrase()); 
      InputStream stream = entity.getContent(); 
      StringBuilder sb = new StringBuilder(); 
      String line; 
      try { 
       br = new BufferedReader(new InputStreamReader(stream)); 
       while ((line = br.readLine()) != null) { 
        sb.append(line); 
       } 
      } catch (Exception e) { 
      } 
      String resp = sb.toString(); 
      System.out.println("response " + resp); 
     } catch (Exception ex) { 
      System.out.println("linked in HttpResponse Error: " + ex); 
     } finally { 
      httppost.releaseConnection(); 
     } 

和授權URL(實際客戶端ID代替linkedin_client_id的發送):

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=linkedin_client_id&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp&state=0kcmjj5504tpgb9&scope=r_basicprofile 

有誰看到我在做什麼錯?如果我把這個編譯後的url並粘貼到瀏覽器中,我可以檢索一個訪問令牌而不會有任何問題。我的要求有問題嗎?

+0

由於它正在vi直接url,你有沒有試圖改變你的請求從發佈到看看會發生什麼? –

+0

我嘗試了一個獲取請求,結果相同。 – kmb71123

+0

您確信您已配置https:// localhost:8090/ProfileSetup/linkedInAuth。jsp是LinkedIn應用程序配置中的有效重定向目標,位於https://www.linkedin.com/developer/apps? –

回答

0

看起來好像您在URL中包含請求的所有參數作爲屬性,而不是在POST正文中包含x-www-form-urlencoded元素。

看看這個其他協議棧線程如何在請求主體發送的值,而不是作爲URL屬性的詳細信息:

Sending HTTP POST Request In Java

0

我終於能弄清楚什麼是錯的我的應用程序。 https的本地環境配置不正確。將代碼移動到我們用https設置的開發框中解決了問題。

相關問題