2012-08-01 42 views
1

我試圖使用OAuth的CX公開的API,我也跟着他們的文檔,還是我越來越HTTP「錯誤的請求」的錯誤,這裏是代碼 -的Oauth不工作CX API

String method = "POST"; 
    String code = ""; 
    NameValuePair[] data = { 
          new NameValuePair("grant_type", "authorization_code"), 
          new NameValuePair("code", code), 
          new NameValuePair("redirect_uri",URLEncoder.encode(CALLBACK_URL, "UTF-8"))    
          }; 
    String secret = CONSUMER_KEY+":"+CONSUMER_SECRET; 
    String encodedSecret = Base64.encodeBase64String(secret.getBytes("UTF-8")); 
    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); 
    PostMethod httpMethod = new PostMethod(ACCESS_TOKEN_ENDPOINT_URL); 
    httpMethod.addRequestHeader("Authorization","Basic "+encodedSecret); 
    httpMethod.setRequestBody(data); 
    System.out.println("HTTP call -- " + method + " " + ACCESS_TOKEN_ENDPOINT_URL); 
    httpClient.executeMethod(httpMethod); 

謝謝, Hemant

+0

請提供一個API文檔的鏈接,因爲它並不完全清楚您所指的提供商「CX」。 – 2012-08-01 16:47:15

+0

它在這裏 - http://developer.cx.com/docs – Hemant 2012-08-06 06:55:27

+1

你是否嘗試過使用不同的OAuth提供程序的相同代碼,只是爲了驗證問題出在CX上? – bruz 2012-08-07 20:26:12

回答

0

我測試了以下代碼的輕微修改,它的工作原理。你可能會仔細檢查

  1. 您的密鑰已被批准(這不應該成爲問題給出的 錯誤您所看到的)。
  2. 你使用了正確的ACCESS_TOKEN_ENDPOINT_URL
  3. 嘗試具有REDIRECT_URI是兩個AUTH_CODE響應和令牌請求

    String method = "POST"; 
        String authCode = "[AUTH-CODE-HERE]"; 
        String CONSUMER_KEY="[YOUR-KEY-HERE]"; 
        String CONSUMER_SECRET="[YOUR-SECRET-HERE]"; 
        String ACCESS_TOKEN_ENDPOINT_URL="https://api.cx.com/1/oauth/token"; 
        String REDIRECT_URI="[YOUR-REDIRECT-HERE]"; 
    
        NameValuePair[] data = { 
          new NameValuePair("grant_type", "authorization_code"), 
          new NameValuePair("code", authCode), 
          new NameValuePair("redirect_uri", REDIRECT_URI) 
        }; 
        String secret = CONSUMER_KEY+":"+CONSUMER_SECRET; 
        String encodedSecret = Base64.encodeBase64String(secret.getBytes("UTF-8")); 
    
        PostMethod httpMethod = new PostMethod(ACCESS_TOKEN_ENDPOINT_URL); 
        httpMethod.addRequestHeader("Authorization","Basic "+encodedSecret); 
        httpMethod.setRequestBody(data); 
        System.out.println("HTTP call -- " + method + " " + ACCESS_TOKEN_ENDPOINT_URL); 
        int responseCode = httpClient.executeMethod(httpMethod); 
        System.out.println(responseCode); 
        System.out.println(httpMethod.getResponseBodyAsString()); 
    

如果你還在運行到的問題是相同的,你可以發佈以下行的結果:System.out.println(httpMethod.getResponseBodyAsString());

0

CX開發人員API已停用。 抱歉給您帶來不便。