Last active
June 20, 2019 10:51
-
-
Save gideonseven/f50a0e531b7fb59cc23a83b26bff6478 to your computer and use it in GitHub Desktop.
Linked In
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final String EXTRA_ID = "PERSON_ID"; | |
public static final String EXTRA_NAME = "PERSON_NAME"; | |
public static final String EXTRA_EMAIL = "PERSON_EMAIL"; | |
public static final String EXTRA_IMAGE = "PERSON_IMAGE"; | |
ApiClient apiClient; | |
/****FILL THIS WITH YOUR INFORMATION*********/ | |
//This is the public api key of our application | |
private static final String API_KEY = "81w4vjx68dhplv"; | |
//This is the private api key of our application | |
private static final String SECRET_KEY = "rTmigBbndYVWbf5t"; | |
//This is any string we want to use. This will be used for avoiding CSRF attacks. You can generate one here: http://strongpasswordgenerator.com/ | |
private static final String STATE = "E3ZYKC1T6H2yP4z"; | |
//This is the url that LinkedIn Auth process will redirect to. We can put whatever we want that starts with http:// or https:// . | |
//We use a made up url that we will intercept when redirecting. Avoid Uppercases. | |
private static final String REDIRECT_URI = "https://deskqoe.id/"; | |
/*********************************************/ | |
//These are constants used for build the urls | |
private static final String AUTHORIZATION_URL = "https://www.linkedin.com/uas/oauth2/authorization"; | |
private static final String ACCESS_TOKEN_URL = "https://www.linkedin.com/uas/oauth2/accessToken"; | |
private static final String SECRET_KEY_PARAM = "client_secret"; | |
private static final String RESPONSE_TYPE_PARAM = "response_type"; | |
private static final String GRANT_TYPE_PARAM = "grant_type"; | |
private static final String GRANT_TYPE = "authorization_code"; | |
private static final String RESPONSE_TYPE_VALUE = "code"; | |
private static final String CLIENT_ID_PARAM = "client_id"; | |
private static final String STATE_PARAM = "state"; | |
private static final String REDIRECT_URI_PARAM = "redirect_uri"; | |
/*---------------------------------------*/ | |
private static final String QUESTION_MARK = "?"; | |
private static final String AMPERSAND = "&"; | |
private static final String EQUALS = "="; | |
/** | |
* Method that generates the url for get the access token from the Service | |
* | |
* @return Url | |
*/ | |
private static String getAccessTokenUrl(String authorizationToken) { | |
return ACCESS_TOKEN_URL | |
+ QUESTION_MARK | |
+ GRANT_TYPE_PARAM + EQUALS + GRANT_TYPE | |
+ AMPERSAND | |
+ RESPONSE_TYPE_VALUE + EQUALS + authorizationToken | |
+ AMPERSAND | |
+ CLIENT_ID_PARAM + EQUALS + API_KEY | |
+ AMPERSAND | |
+ REDIRECT_URI_PARAM + EQUALS + REDIRECT_URI | |
+ AMPERSAND | |
+ SECRET_KEY_PARAM + EQUALS + SECRET_KEY; | |
} | |
======================== | |
/** | |
* Method that generates the url for get the authorization token from the Service | |
* | |
* @return Url | |
*/ | |
private static String getAuthorizationUrl() { | |
return AUTHORIZATION_URL | |
+ QUESTION_MARK + RESPONSE_TYPE_PARAM + EQUALS + RESPONSE_TYPE_VALUE | |
+ AMPERSAND + CLIENT_ID_PARAM + EQUALS + API_KEY | |
+ AMPERSAND + STATE_PARAM + EQUALS + STATE | |
+ AMPERSAND + REDIRECT_URI_PARAM + EQUALS + REDIRECT_URI | |
+ AMPERSAND + " scope=r_fullprofile%20r_emailaddress%20w_share"; | |
} | |
=========================== | |
private void getLinkedInPerson(String token) { | |
apiClient = new ApiClient(); | |
Call<Person> call = apiClient.getApiInterface().getLinkedInPerson("Bearer " + token, "json"); | |
call.enqueue(new Callback<Person>() { | |
@Override | |
public void onResponse(@NonNull Call<Person> call, @NonNull Response<Person> response) { | |
if (response.isSuccessful()) { | |
Person person = response.body(); | |
Log.wtf("PERSON --- ", person.toString()); | |
Log.wtf("FIRSTN --- ", person.getFirstName()); | |
Log.wtf("EMAILAD --- ", person.getEmailAddress()); | |
Log.wtf("PICT --- ", person.getPictureUrl()); | |
/* ** getLinkedInPerson | |
sending intent to SingIN or SignUp | |
*/ | |
Intent replyIntent = new Intent(); | |
replyIntent.putExtra(EXTRA_ID, person.getId()); | |
replyIntent.putExtra(EXTRA_NAME, person.getFirstName()); | |
replyIntent.putExtra(EXTRA_EMAIL, person.getEmailAddress()); | |
replyIntent.putExtra(EXTRA_IMAGE, person.getPictureUrl()); | |
setResult(Activity.RESULT_OK, replyIntent); | |
finish(); | |
} | |
} | |
@Override | |
public void onFailure(@NonNull Call<Person> call, @NonNull Throwable t) { | |
Log.wtf("THROW", t.getLocalizedMessage()); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment