Java 无法获取易趣OAuth访问令牌
我正在尝试获取易趣OAuth访问和刷新令牌,但不断收到401未经授权的响应。我已经阅读了所有关于这个的文档,尝试了所有的东西,但没有任何乐趣 我已经通过了用户权限流,授予了对我的应用程序的访问权限,并拥有授权代码——我现在正在手动将其粘贴到我的代码中,并尝试了URL编码和URL解码,但结果相同 我不确定问题是出在我的java代码还是eBay开发者帐户中的某个值上。任何想法或建议都是非常受欢迎的Java 无法获取易趣OAuth访问令牌,java,oauth,ebay-api,Java,Oauth,Ebay Api,我正在尝试获取易趣OAuth访问和刷新令牌,但不断收到401未经授权的响应。我已经阅读了所有关于这个的文档,尝试了所有的东西,但没有任何乐趣 我已经通过了用户权限流,授予了对我的应用程序的访问权限,并拥有授权代码——我现在正在手动将其粘贴到我的代码中,并尝试了URL编码和URL解码,但结果相同 我不确定问题是出在我的java代码还是eBay开发者帐户中的某个值上。任何想法或建议都是非常受欢迎的 public int initialiseToken(String clientID, String
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;
}
我有一段代码,用于OAuth客户端凭据(用于访问一般信息),如果有帮助的话 我正在做一些非常相似的事情。我现在坚持使用返回的访问令牌来实际实现ebay方法。如果你做到了,能告诉我吗 依赖性
implementation 'com.squareup.okhttp3:okhttp:3.5.0'
代码
公共类MainActivity扩展了AppCompatActivity{
private static final String clientId = "-";//clientId
private static final String clientSecret = "-";//client secret
private static final String tokenUrl = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
private static final String auth = clientId + ":" + clientSecret;
private static final String authentication = Base64.encodeToString(auth.getBytes(),Base64.NO_WRAP);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + authentication)
.url(tokenUrl)
.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded")
,"grant_type=client_credentials"+
"&scope=https://api.ebay.com/oauth/api_scope"
)
)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("Testing_", "Error: " +e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
JSONObject data = null;
try {
data = new JSONObject(json);
String accessToken = data.optString("access_token");
String refreshToken = data.optString("refresh_token");
Log.d("Testing_", "Access Token = " + accessToken);
Log.d("Testing_", "Refresh Token = " + refreshToken);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
谢谢Michael-当我再次拿起它时,我会看一看。同时,这里有一些我用来测试搜索取消界面的代码-希望它对您有所帮助
public static void searchCancellations(String authToken)
{
String urlString = "https://api.ebay.com/post-order/v2/cancellation/search";
String urlStringParams = "?creation_date_range_from=2018-12-01T00:00:00.000Z&creation_date_range_to=2019-01-16T00:00:00.000Z";
try
{
String encodedUrl = urlString + URLEncoder.encode(urlStringParams, "UTF-8");
URL url = new URL(encodedUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Charset", "utf8");
conn.setRequestProperty("Authorization", "IAF " + authToken);
conn.setRequestProperty("Content-Type", "application/json");
int responseCode = 0;
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();
parseCancellationResult(line);
responseCode = 0;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}