【发布时间】:2019-06-02 01:48:50
【问题描述】:
我正在尝试获取 eBay OAuth 访问和刷新令牌,但不断收到 401 Unauthorized 响应。我已经阅读了有关这方面的所有文档,并且几乎尝试了所有方法,但没有任何乐趣。
我已经完成了用户权限流程,授予了对我的应用程序的访问权限并拥有了授权代码 - 我现在手动将其粘贴到我的代码中,并尝试了 URL 编码和 URL 解码,但结果相同。
我不确定问题出在我的 java 代码还是我的 eBay 开发人员帐户中的某个值。任何想法或指示都将受到欢迎。
public int initialiseToken(String clientID, String clientSecret, String ruName)
{
int responseCode = 0;
try
{
String urlString = "https://api.ebay.com/identity/v1/oauth2/token";
String clientCredentials = clientID + ":" + clientSecret;
// base64 encode credentials
byte[] base64clientCredentials = Base64.encodeBase64(clientCredentials.getBytes());
// below authCode obtained from URI redirect following eBay auth sign-in
String authCodeURLEncoded = "v%5E1.1%23i%5E1%23I%5E3%23f.....xI0VeMjYw";
String authCodeURLDecoded = URLDecoder.decode(authCodeURLEncoded, "UTF-8");
URL url = new URL(urlString);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + base64clientCredentials);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Charset", "utf8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("grant_type", "authorization_code");
conn.setRequestProperty("redirect_uri", ruName);
conn.setRequestProperty("code", authCodeURLDecoded); // have tried both encoded & decoded versions
String msg;
if (conn.getResponseCode() != 200)
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
}
else
{
responseCode = conn.getResponseCode();
msg = conn.getResponseMessage();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String line = br.readLine();
parseResult(line);
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return responseCode;
}
【问题讨论】: