Skip to content

Instantly share code, notes, and snippets.

@xcommerce-gists
Last active August 12, 2016 08:59
Show Gist options
  • Save xcommerce-gists/4519761 to your computer and use it in GitHub Desktop.
Save xcommerce-gists/4519761 to your computer and use it in GitHub Desktop.
PayPalAccessOpenIDConnectOAuth.java
//download the complete java code sample from https://github.com/paypal/paypal-access
/**
* Gets Access token by going to token service. Code is left blank so that user can fill it up manually.
*
* @return - Access token
*/
private static String getAccessToken(String authorizationCode) {
StringBuilder tokenUrl = new StringBuilder(
"https://api.paypal.com/v1/identity/openidconnect/tokenservice");
tokenUrl.append("?grant_type=authorization_code");
// code should be obtained manually and pasted here.
tokenUrl.append("&code=" + authorizationCode);
Map<String, Object> responseMap = getResponse(tokenUrl.toString(), "POST", "Basic " + getAuthorizationHeader());
return (String) responseMap.get("access_token");
}
//download the complete java code sample from https://github.com/paypal/paypal-access
/**
* Creates an authorization URL so that user can paste this in browser to get authorization code.
*
* @return - Authorization URL
*/
private static String getAuthorizationUrl() {
StringBuilder authUrl = new StringBuilder(
"https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize");
// all the below params should be changed according to your needs. Below values are just an example.
authUrl.append("?client_id=<REPLACE ME>");
authUrl.append("&response_type=code");
authUrl.append("&scope=openid profile email address");
authUrl.append("&nonce=" + createNonce());
authUrl.append("&redirect_uri=<URL GIVEN WHILE REGISTERING THE APP>");
return authUrl.toString();
}
//download the complete java code sample from https://github.com/paypal/paypal-access
/**
* Gets user info based on <code>accesstoken</code> passed.
*
* @param accessToken - Access token acquired via token service.
* @return - User Information map.
*/
private static Map<String, Object> getUserInfo(String accessToken) {
StringBuilder userInfoUrl = new StringBuilder(
"https://api.paypal.com/v1/identity/openidconnect/userinfo");
userInfoUrl.append("?schema=openid");
return getResponse(userInfoUrl.toString(), "GET", ("Bearer " + accessToken));
}
@adityasd
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment