This is my first blog,I hope this blog is really beneficial for you guys.
In Android there are certain steps needed to connect android mobile app with Facebook.Steps are provided below.
1. Download the android sdk from link " https://github.com/facebook/facebook-android-sdk" and extract the zip for windows,In src there is folder Facebook called,extract this folder.It will contain six java files like AsyncFacebookRunner.java,Facebook.java,Util.java etc.
2.Create android project in your android application,and paste the facebook folder of sdk in your src folder.It might give some error,So please change the package name of the classes,It will automatically remove all the errors.
3.Now open your facebook account,then there is a developer option in your facebook page footer.click on it ,it will redirect developer account of your facebook account,then select build for mobile It will open a new developer page,then Click on "app" on header of this page.it will show the app page then select "create new app" ,then a dialog will appear,fill the name that you want for your facebook app in Display name,the app name is visible in all the sharing done by this app using your android mobile.So kindly provide some meaningful vivid name.after certain processing a setting page appear for your newly created app,Where you can change the icon for android app as well as display name also.It's not worthy to discuss these things here,So we return back to our work,Now select Native Android App,It will ask for Android Hash key,This is more rigorous thing in order to connect with facebook,So kindly follow the substeps given below.
(a)Download openssl from http://code.google.com/p/openssl-for-windows/downloads/detail?name=openssl-0.9.8k_WIN32.zip if you hace 32 bit window machine,otherwise download 64 bit.you find a zip file .please extract it in C: directory and rename it as "openssl".
(b) there are two mode for android mode,the first one is debug mode,and the another one is signed app,the Hash key for both are different,So kindly keep these things in mind otherwise It will waste your huge amount of time.
FOR DEBUG ANDROID APP:
open command prompt and go in the java installed directory,like I have
installed the java in c drive,So for me command is:
cd C:\Program Files\Java\jdk1.6.0\bin;
then type the command,
keytool -exportcert -alias androiddebugkey -keystore
"C:\Documents and Settings\Administrator.android\debug.keystore"
| "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64
It will ask for password please provide password,for debug mode password is "android"
it will return your generated Hash key.In some window machine -exportcert does not work So kindly use -export in place of -exportcert.Enter this Hash key in your
Facebook app Hash key.
FOR SIGNED ANDROID APP:
the above command also work,but with some little modification.
keytool -exportcert -alias "alias name of your keystore" -keystore
"path of keystore file" | "C:\OpenSSL\bin\openssl" sha1 -binary |
"C:\OpenSSL\bin\openssl" base64 It ask for password,then provide password of
your keystore,
after providing password it will generate the android Hash key
provide this Hash key in your Facebook app.
But this was not worked in so many cases,SO kindly put the following code inside your
Acivity to see the Hash key.
PackageInfo info = getPackageManager().getPackageInfo("packageName",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String something = new String(Base64.encode(md.digest(), 0));
AlertDialog.Builder ab=new AlertDialog.Builder(this);
ab.setMessage(something);
ab.show();
ab.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
}
}
catch(Exception e)
{
}
when you run your exported apk in your android device,then it will show the Hash
key in dialog ,copy the key in your Facebook app.
In Facebook app we can give more than one Hash key.
(4)Now the We discuss Code part in your android project for sharing resources
with Facebook.First declare internet permkission in Androidmanifest.xml like below.
change the authorize function that is declared on line no 181 in authorize function
of Facebook Class in your project,that I am providing below.
Line number can be change,I have seen line number in Eclipse.
***************************************************************************
int activityCode, final DialogListener listener) {
boolean singleSignOnStarted = false;
mAuthDialogListener = listener;
// Prefer single sign-on, where available.
/* if (activityCode >= 0) {
singleSignOnStarted = startSingleSignOn(activity, mAppId,
permissions, activityCode);
}*/
// Otherwise fall back to traditional dialog.
if (!singleSignOnStarted) {
startDialogAuth(activity, permissions);
}
}
(5)In your Activity called Home.java write the following code.
public class Index extends Activity implements OnClickListener {
public static final String APP_ID = "350220418325230";//app id you can get it from
facebook app
Facebook mfacebook = null;
Button submit;(Facebook button declared in layout of this activity)
Button logout;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mfacebook = new Facebook(APP_ID);
submit=(Button)findViewById(R.id.btnsubmit);
submit.setOnClickListener(this);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mfacebook.logout(Index.this);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
mfacebook.authorize(Index.this, new String[] { "publish_stream" },
new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
postToWall(values.getString(Facebook.TOKEN));
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
private void postToWall(String accessToken) {
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
// The byte array is the data of a picture.
params.putByteArray("picture", bytes);//bytes is byte Array for Image.
params.putString("message", "Hello");
params.putString("description", ed.getText().toString());
try {
mfacebook.request("me/photos", params, "POST");
} catch (FileNotFoundException fileNotFoundException) {
// makeToast(fileNotFoundException.getMessage());
} catch (MalformedURLException malformedURLException) {
// makeToast(malformedURLException.getMessage());
} catch (IOException ioException) {
// makeToast(ioException.getMessage());
}
catch (Exception e) {
// TODO: handle exception
setAlertDialog1("Either User name,password,storage or Network error");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mfacebook.authorizeCallback(requestCode, resultCode, data);
}
}
I share the code for sharing image,you can share text with little modification in
this code.In thiis code you have to find out byteArray for images that you want to
share,I did not define bytes for byteArray,but you guys have to first declare
the bytes,Then it will work fine.I have tested this code on mu HTC widfire S and
Samsung Galaxy s2,It will work for all android version upper than
api level 7.
I think this blog will helpful for sharing images on Facebook,
please share your thoughts with us,If there is any problem then feel free to contact me
on my emailid ashwani.tiwari90@gmail.com.
Sir,
ReplyDeletePlease tell me what is the need of openSSL and key-tool.What is the mean of Android Mode like debug mode and signed mode.
Dear jitendra ,
ReplyDeletekey-tool is library provided by java distribution team,to get signed hash key
for app and openssl provide the algo for signing,
Like I have used sha1 algo...
This comment has been removed by the author.
ReplyDeleteHello sir, if i want to Integrate like pages integration of Facebook in my Android apps so which steps i had to follows please help me.
ReplyDelete