我想向 Neteller 设置成功的请求,我正在尝试使用 Neteller 文档中的代码获取访问 token 。但是,它始终失败,但有以下异常(exception):
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();
}
为什么我的实现在这里失败?
最佳答案
你的代码没有任何问题。问题是您正在尝试使用普通成员(member)帐户进行 API 集成,而您需要使用商家帐户来进行集成。以下是您需要完成的步骤才能使其正常工作:
- 您需要获取一个测试商家帐户 ( http://www.neteller.com/business/contact-sales/ )。在 www.neteller.com 上注册会创建一个普通成员(member)帐户,该帐户无法通过 API 接收付款。
- 拥有测试商家帐户后,您需要将向 API 发出请求的 IP 地址列入白名单。 (手册第 31 页)。
- 然后,您需要向其中添加一个应用程序(手册第 32 页)。
- 添加应用程序后,请在授权 header 中使用“客户端 ID”和“客户端 key ” - 就像您现在所做的那样,base64 编码值,用冒号 (:) 分隔。
关于java - 尝试从 Neteller 获取 OAuth 访问 token 会产生错误 : "Server returned HTTP response code: 401 for URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29097264/