I’m trying to obtain an eBay OAuth access & refresh token but keep getting a 401 Unauthorized response. I’ve been through every bit of documentation on this and tried just about everything but no joy.
I’ve been through the user permission flow, granted access to my application and have the authorize code – I’m manually pasting this into my code for now and have tried it both URL encoded and URL decoded but the same result.
I’m not sure if the problem lies with my java code or one of the values in my eBay developer account. Any ideas or pointers would be most welcome.
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;
}
2
Answers
Thanks Michael - will take a look at that when I pick this up again. In the meantime, here's some code that I've used to test the Search Cancellations interface - hopefully it may be of some use to you
I have a piece of code that’s for OAuth Client credential ( for accessing general information) if it helps.
I am working on something very similar. I am right now stuck on using the returned access token to actually implement an ebay method. If you have managed to do that, can you let me know?
Dependency
Code
public class MainActivity extends AppCompatActivity {
Output