Oauth不工作CX api

时间:2012-08-01 10:15:54

标签: oauth-2.0

我正在尝试将oauth用于CX暴露的api,我按照他们的文档,我仍然得到HTTP“BAD REQUEST”错误,这是代码 -

    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

2 个答案:

答案 0 :(得分:0)

我测试了以下对代码的轻微修改,它确实有效。您可以仔细检查

  1. 您的密钥已获批准(这不应该是给出的问题 你看到的错误。)
  2. 您使用的是正确的ACCESS_TOKEN_ENDPOINT_URL
  3. 尝试让auth_code响应和令牌请求的redirect_uri相同

        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());
    
  4. 如果您仍然遇到问题,可以发布以下行的结果:System.out.println(httpMethod.getResponseBodyAsString());

答案 1 :(得分:0)

CX开发人员API已停止使用。 很抱歉给您带来不便。