Skip to content

Instantly share code, notes, and snippets.

@gounthar
Created May 21, 2021
Embed
What would you like to do?
OAuthLogin singleton = new OAuthLogin();
try {
CredentialUtil.load(singleton.getClass().getClassLoader().getResource("ebay-config-sample.yaml").openStream());
} catch (IOException e) {
e.printStackTrace();
}
OAuth2Api oauth2Api = new OAuth2Api();
Environment environment = Environment.PRODUCTION;
String refreshToken = "";
List<String> scopes = null;
try {
scopes = Arrays.asList(URLEncoder.encode("https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly", StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build();
String url = oauth2Api.generateUserAuthorizationUrl(environment, scopes, Optional.ofNullable(refreshToken));
System.out.println(url);
HttpContext context = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
response = client.execute(httpPost, context);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response.getStatusLine().getStatusCode());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
System.out.println(currentReq.toString());
HttpHost currentHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
System.out.println(currentHost.toString());
String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment