我正在尝试获取eBay OAuth访问和刷新令牌,但一直收到401未经授权的响应。我已经遍历了所有文档,并尝试了几乎所有内容,但没有任何乐趣。
我已经完成了用户许可权流程,获得了访问我的应用程序的权限,并拥有了授权代码-我现在手动将其粘贴到我的代码中,并尝试将其进行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;
}
答案 0 :(得分:0)
如果有帮助,我有一段代码适用于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();
}
}
});
}
答案 1 :(得分:0)
谢谢迈克尔-我再次讲解时会看一下。同时,这是我用来测试“搜索取消”界面的一些代码-希望它可能对您有用
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();
}
}