我想向Neteller设置成功的请求,我试图使用Neteller文档中的代码获取访问令牌。但是,它始终失败,但有以下异常:

java.io.IOException: Server returned HTTP response code: 401 for URL: https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials


这是代码(同样,来自Neteller文档):

    String testUrl = " https://test.api.neteller.com";
    String secureUrl = "https://api.neteller.com";
    String url = testUrl;
    if("live".equals(configBean.get("environment"))){
        url = secureUrl;
    }
    url += "/v1/oauth2/token?grant_type=client_credentials";
    String xml = "grant_type=client_credentials?grant_type=client_credentials";
    xml = "";
    String test = Base64.encodeBytes((accountID + ":" + secureID).getBytes());
    try {
        URL urls = new URL ("https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials");
        HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty  ("Authorization", "Bearer " + test);
        connection.setRequestProperty  ("Content-Type", "application/json");
        connection.setRequestProperty  ("Cache-Control", "no-cache");
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.flush();
        wr.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String accessToken = "";
    } catch(Exception e) {
        e.printStackTrace();
    }


为什么我的实现在这里失败?

最佳答案

您的代码没有错。问题是您正在尝试使用常规成员帐户进行API集成,而您需要在该帐户中使用商家帐户。为了使它正常工作,您需要完成以下步骤:


您需要获得测试商家帐户(http://www.neteller.com/business/contact-sales/)。在www.neteller.com上注册会创建一个普通会员帐户,该帐户无法通过API接收付款。
拥有测试商家帐户后,您需要将要向其发出API请求的IP地址列入白名单。 (手册第31页)。
然后,您将需要向其中添加一个应用程序(手册第32页)。
添加应用程序后,请在Authorization标头中使用“客户端ID”和“客户端密码”-就像现在一样,使用base64编码的值,并用冒号(:)分隔。

09-05 20:33