Skip to content

Instantly share code, notes, and snippets.

@mauriciogior
Last active December 19, 2018 06:41
Show Gist options
  • Save mauriciogior/5587fa8170b980a78516 to your computer and use it in GitHub Desktop.
Save mauriciogior/5587fa8170b980a78516 to your computer and use it in GitHub Desktop.
SignIn with LinkedIn Android (retrieving email address)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aa000000">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="40dp"/>
<FrameLayout android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="5dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="22dp"
android:background="@drawable/close_button">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_action_cancel_light"/>
</FrameLayout>
</RelativeLayout>
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.enumeration.ProfileField;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
import com.google.code.linkedinapi.schema.Person;
/* DON'T FORGET TO PUT THIS ON YOUR MANIFEST:
<activity
android:name="com.sample.linkedin.LinkedInActivity"
android:label="@string/app_name"
android:theme="@style/Transparent"
android:launchMode="singleInstance"
android:parentActivityName="com.sample.linkedin.SignInActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="x-oauthflow-linkedin" android:host="com.sample.linkedin" />
</intent-filter>
</activity>
*/
/*
Download LinkedIn-J with Email-address ready: https://www.dropbox.com/s/2f0ad4v5ovbbp3t/LinkedIn-J%20%2B%20email.zip
*/
public class LinkedInActivity extends FragmentActivity
{
protected static final String TAG = "LinkedInActivity";
public Fragment currentFragment = null;
public LinkedInOAuthService oAuthService;
public LinkedInApiClientFactory factory;
public LinkedInRequestToken liToken;
public ProgressDialog dialog = null;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_fragment);
if (savedInstanceState == null)
{
currentFragment = new LinkedInFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, currentFragment).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
getSupportFragmentManager()
.beginTransaction()
.remove(currentFragment)
.commit();
dialog = ProgressDialog.show(this, "","Signing in, please wait...", true);
String verifier = intent.getData().getQueryParameter("oauth_verifier");
class LinkedInToken extends AsyncTask<String, String, String>
{
Person person;
String verifier;
LinkedInAccessToken accessToken;
public LinkedInToken(String verifier) { this.verifier = verifier; }
@Override
protected String doInBackground(String... params)
{
accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
LinkedInApiClient client = factory.createLinkedInApiClient(
accessToken.getToken(),
accessToken.getTokenSecret());
// GRAB PERSON DATA, WITH EMAIL ADDRESS!!
person = client.getProfileForCurrentUser(EnumSet.of(
ProfileField.ID, ProfileField.FIRST_NAME,
ProfileField.LAST_NAME, ProfileField.PICTURE_URL,
ProfileField.EMAIL_ADDRESS
));
return null;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
dialog.dismiss();
//DO SOMETHING WITH PERSON
}
}
new LinkedInToken(verifier).execute("");
}
}
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
public class LinkedInFragment extends Fragment
{
private View rootView = null;
private SignInWithLinkedInActivity mainActivity = null;
private WebView browser;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mainActivity = (SignInWithLinkedInActivity) getActivity();
rootView = inflater.inflate(R.layout.fragment_linkedin,
container, false);
browser = (WebView) rootView.findViewById(R.id.webview);
browser.setVerticalScrollBarEnabled(true);
browser.setHorizontalScrollBarEnabled(true);
browser.requestFocusFromTouch();
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setBuiltInZoomControls(true);
FrameLayout close = (FrameLayout) rootView.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mainActivity.finish();
mainActivity.overridePendingTransition(R.anim.none, R.anim.fade_out);
}
});
new LinkedInLogin().execute("");
return rootView;
}
class LinkedInLogin extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
mainActivity.oAuthService = LinkedInOAuthServiceFactory
.getInstance()
.createLinkedInOAuthService("KEY","SECRET");
mainActivity.factory = LinkedInApiClientFactory
.newInstance("KEY","SECRET");
mainActivity.liToken = mainActivity
.oAuthService
.getOAuthRequestToken("x-oauthflow-linkedin://com.estudiotrilha.inevent");
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// WebView doesn't scroll on API <= 11,
// so we open an intent :)
if(Build.VERSION.SDK_INT > 11)
{
browser.loadUrl(mainActivity.liToken.getAuthorizationUrl());
}
else
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(mainActivity.liToken.getAuthorizationUrl()));
startActivity(i);
}
}
}
}
@mauriciogior
Copy link
Author

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