This article helps users to integrate Facebook & Linkedin functionality easily in Android apps.
Background
While we creating Android apps, it is again and again requirement to be able to post status updates on social networks like Facebook & LinkedIn. The user experience is that users are first taken to a page where they can login to Facebook or LinkedIn. Then the control returns back to the application which can now post messages on their behalf.
There are multiple ways of doing the integration. One is to use the SDKs available from Facebook, Linkedin etc. which provide the API for sharing these. Second is to use open an embedded browser control and use OAuth for authentication and finally the REST APIs provided to post the update. Both of these are slightly tedious, since either you have to download and use the different APIs or you have to implement the complete protocol.
Getting Started for
Facebook
Integrate SDK in App
Now we can start integrating the SDK which can be downloaded from Download. This SDK contains the java libraries that do the heavy lifting of OAuth
as well as the REST calls for each social provider. Extract the contents
and we are all set.
Copy the libs/socialauth-vincent.jar and libs/socialauth 2.3 jar into the libs folder of your application. If you have latest ADT, jars will
be automatically added to your build path else you need to manually add
the jar files in build path. We recommend you to upgrade to the latest
ADT.
Copy the assets/oauth_consumer.properties file to the assets folder
of your application. The file is used by the SDK for the keys and secrets.
Replace the default key and secret with your key and secret that were
obtained above.
Add android.permission.INTERNET and
android.permission.ACCESS_NETWORK_STATE in manifest.
Implementation
Create
a button in your application and the behavior on click is supplied by the SDK
through a
Following
is the code for creating the adapter and attaching it with the button.
You
also need to provide a
Getting Started for Linkedin
Integrate SDK in App
Now we can start integrating the SDK which can be downloaded from Download. This SDK contains the java libraries that do the heavy lifting of OAuth
as well as the REST calls for each social provider. Extract the contents
and we are all set.
Copy the libs/socialauth-vincent.jar and libs/socialauth 2.3 jar into the libs folder of your application. If you have latest ADT, jars will
be automatically added to your build path else you need to manually add
the jar files in build path. We recommend you to upgrade to the latest
ADT.
Copy the assets/oauth_consumer.properties file to the assets folder
of your application. The file is used by the SDK for the keys and secrets.
Replace the default key and secret with your key and secret that was obtained
above.
Add android.permission.INTERNET and
android.permission.ACCESS_NETWORK_STATE in manifest.
Implementation
Create
a button in your application and the behavior on click is supplied by the SDK
through a
Following
is the code for creating the adapter and attaching it with the button.
You
also need to provide a
Background
While we creating Android apps, it is again and again requirement to be able to post status updates on social networks like Facebook & LinkedIn. The user experience is that users are first taken to a page where they can login to Facebook or LinkedIn. Then the control returns back to the application which can now post messages on their behalf.
There are multiple ways of doing the integration. One is to use the SDKs available from Facebook, Linkedin etc. which provide the API for sharing these. Second is to use open an embedded browser control and use OAuth for authentication and finally the REST APIs provided to post the update. Both of these are slightly tedious, since either you have to download and use the different APIs or you have to implement the complete protocol.
Fortunately there is an open
source SDK available that is easy to use and allows integrating with several
social networks. This is known as SocialAuth Android SDK. In addition to
posting
status updates, you can also get user profile from Facebook, Twitter and LinkedIn using the API, which will allow you to register users as well.
status updates, you can also get user profile from Facebook, Twitter and LinkedIn using the API, which will allow you to register users as well.
Getting Started for
Facebook
First you need to register
your application with the social provider you need to integrate, and get
the API key and secret.
Step1. Go to URL - http://www.facebook.com/developers and fill username and password. Create account if you don't have one.
Step1. Go to URL - http://www.facebook.com/developers and fill username and password. Create account if you don't have one.
4. Note down keys and secrets and copy them in
oauth_consumers.properties file.
Integrate SDK in App
Now we can start integrating the SDK which can be downloaded from Download. This SDK contains the java libraries that do the heavy lifting of OAuth
as well as the REST calls for each social provider. Extract the contents
and we are all set.
Copy the libs/socialauth-vincent.jar and libs/socialauth 2.3 jar into the libs folder of your application. If you have latest ADT, jars will
be automatically added to your build path else you need to manually add
the jar files in build path. We recommend you to upgrade to the latest
ADT.
Copy the assets/oauth_consumer.properties file to the assets folder
of your application. The file is used by the SDK for the keys and secrets.
Replace the default key and secret with your key and secret that were
obtained above.
Add android.permission.INTERNET and
android.permission.ACCESS_NETWORK_STATE in manifest.
Implementation
Create
a button in your application and the behavior on click is supplied by the SDK
through a SocialAuthAdapter
object.
Following
is the code for creating the adapter and attaching it with the button.
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnshare = (Button)findViewById(R.id.btnshare);
btnshare.setText("Share");
adapter = new SocialAuthAdapter(new ResponseListener());
adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
adapter.enable(btnshare);
}
|
You
also need to provide a ResponseListener
to do the
handling once the authentication is complete, for example, to post updates.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { Log.d("ShareButton" , "Authentication Successful"); adapter.updateStatus("http://www.vincentit.com/ "); } |
Getting Started for Linkedin
First you need to register your application with the social
provider you need to integrate, and get the API key and secret.
Enter the Email and Password, and click on OK, I’ll Allow
It.
Click on Support and select API Keys.
Click Add New Application
Note down keys and secrets and copy them in
oauth_consumers.properties file.
Integrate SDK in App
Now we can start integrating the SDK which can be downloaded from Download. This SDK contains the java libraries that do the heavy lifting of OAuth
as well as the REST calls for each social provider. Extract the contents
and we are all set.
Copy the libs/socialauth-vincent.jar and libs/socialauth 2.3 jar into the libs folder of your application. If you have latest ADT, jars will
be automatically added to your build path else you need to manually add
the jar files in build path. We recommend you to upgrade to the latest
ADT.
Copy the assets/oauth_consumer.properties file to the assets folder
of your application. The file is used by the SDK for the keys and secrets.
Replace the default key and secret with your key and secret that was obtained
above.
Add android.permission.INTERNET and
android.permission.ACCESS_NETWORK_STATE in manifest.
Implementation
Create
a button in your application and the behavior on click is supplied by the SDK
through a SocialAuthAdapter
object.
Following
is the code for creating the adapter and attaching it with the button.
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnshare = (Button)findViewById(R.id.btnshare);
btnshare.setText("Share");
adapter = new SocialAuthAdapter(new ResponseListener());
adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
adapter.enable(btnshare);
|
}