1

我正在尝试将 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);

谢谢,赫曼特

4

2 回答 2

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());
    

如果您仍然遇到问题,您能否发布以下行的结果: System.out.println(httpMethod.getResponseBodyAsString());

于 2012-08-07T22:13:52.693 回答
0

CX 开发人员 API 已停止使用。带来不便敬请谅解。

于 2013-10-28T21:48:00.820 回答