簡體   English   中英

鏈接中無法檢索缺少所需參數的訪問令牌錯誤

[英]Linked in unable to to retrieve access token missing required parameters error

我一直在通過遵循這里的文檔將我的應用程序與LinkedIn集成。 我已經在LinkedIn中創建了我的應用程序,並且能夠成功檢索授權代碼,但是在嘗試獲取訪問令牌時出現以下錯誤:

{“ error_description”:“缺少必需的參數,包括無效的參數值,參數多次。。:無法檢索訪問令牌:appId或重定向uri與授權碼不匹配或授權碼已過期”,“錯誤”:“ invalid_request” }

我已驗證以下各項是正確的:

  1. 重定向uri與授權請求和訪問令牌請求相同
  2. 我在發布授權碼后的20秒鍾內使用了授權碼。
  3. 我在請求標頭中包含“ Content-Type:application / x-www-form-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並將其粘貼到瀏覽器中,則可以毫無問題地檢索訪問令牌。 我的要求有問題嗎?

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

請查看另一個Stack線程,以獲取有關如何在請求正文中而不是作為URL屬性發送值的詳細信息:

用Java發送HTTP POST請求

我終於能夠弄清楚我的應用程序出了什么問題。 未為https正確配置我的本地環境。 將代碼移到我們設置為https的開發箱中解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM