Facebook Error Invalid API Key - android

I am trying to post an image to facebook wall from my android application. I am using the facebook sdk.
From my MainActivity when i call FBConnectionActivity to get the connection and ID, I get a null pointer exception
I am calling 2 methods like this:-
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setConnection();
getID();
}
}
In the FBConnectionActivity, these two methods are defined as:
public void setConnection() {
mContext = this;
mFacebook = Utility.mFacebook;
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public void getID(TextView txtUserName, ProgressBar progbar) {
username = txtUserName;
pb = progbar;
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
public void getID(){
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
In the call to isSession(), the method call to isSessionValid() method always returns false.
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
Log.e("isSession() method ", "acess_token : " + access_token );
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
public boolean isSessionValid() {
return (getAccessToken() != null) &&
((getAccessExpires() == 0) ||
(System.currentTimeMillis() < getAccessExpires()));
}
If my session is not valid , how do i obtain a new valid access token. Every time i try to upload an image from my application, the method isSessionValid() returns false. Also i am getting the following error:-
01-18 12:46:35.928: D/Facebook-Example(28459): Response: {"error_code":101,"error_msg":"Invalid API key","request_args":[{"key":"method","value":"photos.upload"},{"key":"format","value":"json"}]}
01-18 12:46:35.948: W/Facebook-Example(28459): Facebook Error: Invalid API key
Please help me.
I am using the following method for posting the image on facebook. Is there anything wrong here?
public void postImageonWall() {
Log.e("BrowsePictureActivity ", "Inside postImageonWall method");
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Log.d("BrowsePictureActivity ", "data.length : " + data.length);
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(),
null);
}

You have not posted your code snippet that includes Posting image to Facebook wall
If you map wrong Key and Value Pair you will get Invalid API Key.
So ensure that you are using correct Key and Value Pair for Post image and be careful while you add attachments along with images.
Try this for Posting Image to Facebook wall :
private void postToWall(String accessToken) {
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream); // bm is bitmap object of your image
byte[] byteArray = stream.toByteArray();
Bundle param = new Bundle();
param = new Bundle();
param.putString("message", "All");
param.putString("filename", "Test");
param.putByteArray("image", byteArray);
mAsyncRunner.request("me/photos", param, "POST", new fbRequestListener(), null);
}
public class fbRequestListener implements RequestListener {
#Override
public void onComplete(String response, Object state) {
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
}

Related

Unable to use facebook login in android device

I have same issue mentioned in this stack overflow question. But couldn't find the solution. My problem is when I use facebook login in my android device(which is not installed facebook app), my app works as expected. If I install the facebook application my code doesn't work anymore and It throws the error "Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook". My code is posted below
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAsyncRunner = new AsyncFacebookRunner(facebook);
loginToFacebook();
}
#SuppressWarnings("deprecation")
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
getProfileInformation();
} }); } }
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
/**
* Get Profile information by making request to Facebook Graph API
* */
#SuppressWarnings("deprecation")
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
// Facebook Profile JSON data
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
// getProfileInformation();
String arr[] = name.split(" ", 2);
String name1 = arr[0];
String name2 = arr[1];
Intent intent = new Intent(FacebookLogin.this, UserAccount.class);
/*Sending some arguments*/
Bundle bundle = new Bundle();
bundle.putString("UserName",name1);
bundle.putString("Id", email);
intent.putExtras(bundle);
logoutFromFacebook();
startActivity(intent); }
});
} catch (JSONException e) {
e.printStackTrace();
} } }); }
#SuppressWarnings("deprecation")
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
facebook.setAccessToken(null);
facebook.setAccessExpires(0);
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
} } }); }
}
Please anyone tell me how can I avoid this error...

Post Photo to Facebook from Android Application, Photo taken from Android Gallery

I am currently developing an Android Application target build is 4.0 Ice-Cream Sandwich.
So far, I am able to post a normal Text onto Facebook with this code:
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
However, I am unable to post a photo onto Facebook with Captions. I've search around online and one of the codes I found is this:
public void postToWall() {
// post on user's wall.
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(photoToPost);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
The problem is, the above code is not working as I don't know whats:
1.photoToPost
2.mAsyncRunner.request keeps giving me an error stating that I cannot put "null" as it is an invalid arguement
3.SampleUploadListener, supposedly is from the FacebookSDK is not working as well (I keep getting an error to create a Class)
Is there a simpler code out here ? Or could someone explain to me the errors I am experiencing.
I am using an "On Click" so far to post normal Text onto Facebook and it points to this method. My goal is to upload a Photo with a Caption onto Facebook.
Thank you all for helping !
1-This is your photo that will be send to wall , it can be an image from your SD card or anywhere else
2-This a class from Facebook SDK , that accepts facebook object (one u created before)
3-This a class from Facebook SDK again
it seems there is something wrong with your Facebook SDK
try to set it again using Right Click on Project >> Properties >> Android and see if library exist or not
The problem lies in following line
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
you are passing null as graph path this should be like this = "me/feed"
Update
write this line of code
mAsyncRunner.request("me/feed", params, "POST", new SampleUploadListener(), null);
then it should work.
public class CardShared extends Activity{
public static final String APP_ID = "YOUR APP ID";
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner ;
boolean isLoggedIn = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
//Implementing SSO
mFacebook.authorize(this, new String[]{"publish_stream"}, new DialogListener(){
public void onComplete(Bundle values) {
sharePicture(values.getString(Facebook.TOKEN));
Toast.makeText(getApplicationContext(), "Picture Shared Successfully", Toast.LENGTH_SHORT).show();
CardShared.this.finish();
}
public void onFacebookError(FacebookError e) {
Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
public void onError(DialogError e) {
Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
public void onCancel() {
Log.d("CANCELLED","AUTH CANCELLED");
}
});
}
//updating Status
public void sharePicture(String accessToken){
byte[] data = null;
try {
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image_to_be_uploaded);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params
.putString(Facebook.TOKEN, mFacebook
.getAccessToken());
params.putByteArray("picture", data);
mAsyncRunner.request(null, params, "POST",
new SampleUploadListener(), null);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("onActivityResult","onActivityResult");
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public class SampleUploadListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
try {
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String f = json.getString("src");
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
}
put like this
mAsyncRunner.request("me/feed", params, "POST", new SampleUploadListener())
See this link. That will show you how to post image on FaceBook wall as how to post text on wall.
Its good to learn.

Not able to login for posting image on my Facebook wall

I am using following code to post image on Facebook wall.
it's working fine.
Problem is --> if the device having Facebook Application i am not able to post image on wall.
-->The device does't having Facebook App. it's working without any problems.
please help me where is the problem.
this is the code i am using here.
public class ShareOnFacebook extends Activity {
private static final String APP_ID = "269876589726953";
private static final String[] PERMISSIONS = new String[] {"publish_stream"};
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private String messageToPost;
private Bitmap mBitmap;
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBitmap = CropImage.getBitmapCrop();
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.share);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null){
facebookMessage = "Test wall post";
}
messageToPost = facebookMessage;
}
public void doNotShare(View button){
finish();
}
public void share(View button){
if (! facebook.isSessionValid()) {
loginAndPostToWall();
}
else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}
public void postToWall(String message) {
// posting image on FB wall
byte[] data = null;
Bitmap bi = mBitmap;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new PhotoUploadListener(), null);
finish();
}
public class PhotoUploadListener extends com.facebook.android.BaseRequestListener {
public void onComplete(final String response, final Object state) {
//dialog.dismiss();
//showToast("Image shared on your facebook wall!");
}
public void onFacebookError(FacebookError error) {
//dialog.dismiss();
Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),Toast.LENGTH_LONG).show();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null){
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
private void showToast(String message){
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
I am not sure why you have used null here. As far as I know, instead of this,
mAsyncRunner.request(null, params, "POST", new PhotoUploadListener(), null);
it should be something like this,
mAsyncRunner.request("me/photos", params, "POST", new PhotoUploadListener(), null);
And still if your problem exists because of an existing Facebook app in the device, then you might have make the login compulsory.
In this piece of your code,
public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}
do this change,
public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
It should do the trick.
Small Changes in the code (facebook package)
Look into authorize function in facebook.java file. try to comment out the singlesignon and use startdialog() only.Hope it helps.

Android - How to upload photo from the SD card to the Facebook wall

I use the Facebook Android SDK.
Goal
Create multiple posts in news feed of Facebook logged in user that will contain photo from the Android device (its SD card) and some comment. The result should be the same as when you do it using the Add photo/video feature directly in Facebook. In the end, it should look like this:
Wanted Facebook result
Problem
I can't do it.
I went through the numerous similar posts on Stack Overflow, but no answer there so far.
What I have tried to implement so far
Approach 1: SD card photos 2 Facebook album
How
Upload pictures from my mobile (its SD card) to an album that is created for my application the first time I upload a picture from it. In this case, when constructing the params object, I use the picture key and put the bytes of the picture as its value. I use me/photos in the request(...) call of the Facebook (or AsyncFacebookRunner) object. **
The problem
Not all uploaded images are displayed on my wall. Instead, there is something like x photos were added to the album xxx.
The code snippet is this (for one picture)
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/photos", params, "POST", new PostPhotoRequestListener(), null);
Facebook result
Approach 2: Internet photos 2 facebook news feed
How
Display pictures stored somewhere on the Internet in posts on my wall. In this case, when constructing the params object, I use the link key and set the url to picture as its value. I use me/feed in the request(...) call.
The problem
This produces some strange output, but it isn't what I want.
The code snippet is this (for one picture)
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putString("link", "http://i1114.photobucket.com/albums/k538/tom_rada/bota2.jpg");
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
Facebook result
Approach 3: Mix of approach 1 and 2
How
I try to use the picture key and set photo bytes as its value (as in 1.), and call the request with me/feed (as in 2.),
The problem
Message is produced as I would like it to be, but no photo is included
The code snippet is this (for one picture)
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
Facebook result
So, any ideas how I could reach my goal?
EDIT - WORKAROUND FOUND
It seems that the only way to create new posts containing photos on user's wall is to add photos and related comments to user's Wall photos album.
How - Code snippet
Beware: The facebook.request call should be replaced with async call, so the operation doesn't block the UI thread !!!
String wallAlbumID = null;
String response = facebook.request("me/albums");
JSONObject json = Util.parseJson(response);
JSONArray albums = json.getJSONArray("data");
for (int i =0; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
if (album.getString("type").equalsIgnoreCase("wall")) {
wallAlbumID = album.getString("id");
Log.d("JSON", wallAlbumID);
break;
}
}
... and then
if (wallAlbumID != null) {
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("source", bytes);
asyncRunner.request(wallAlbumID+"/photos", params, "POST", new PostPhotoRequestListener(), null);
}
Facebook facebook = new Facebook("your appid");
private void uploadImage()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
facebook.authorize(FacebookActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener()
{
#Override
public void onComplete(Bundle values)
{
//uploadImageOnlyToWall(data, "Uploading Image only to wall","Test Post from Android while uploading photo with message");
uploadImageToWallAndAlbums(imageUrl, "Image via link");
}
#Override
public void onFacebookError(FacebookError error)
{
Toast.makeText(FacebookActivity.this, "FaceBook Error", Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError e)
{
Toast.makeText(FacebookActivity.this, "Error", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel()
{
Toast.makeText(FacebookActivity.this, "Canceled", Toast.LENGTH_LONG).show();
}
});
}
private void uploadImageOnlyToAlbum(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putByteArray("picture", byteArray);
params.putString("caption",caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
private void uploadImageToWallAndAlbums(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", byteArray);
params.putString("caption", caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
If the user has not previously posted a photo on his/her wall (there is no wall photo album), you can use me/photo request to post a photo first. This will automatically create a wall album.
Facebook facebook = new Facebook("your App_id");
private void uploadImage()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
facebook.authorize(FacebookActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener()
{
#Override
public void onComplete(Bundle values)
{
//uploadImageOnlyToWall(data, "Uploading Image only to wall","Test Post from Android while uploading photo with message");
uploadImageToWallAndAlbums(imageUrl, "Image via link");
}
#Override
public void onFacebookError(FacebookError error)
{
Toast.makeText(FacebookActivity.this, " Error", Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError e)
{
Toast.makeText(FacebookActivity.this, "Error", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel()
{
Toast.makeText(FacebookActivity.this, "Canceled", Toast.LENGTH_LONG).show();
}
});
}
private void uploadImageOnlyToAlbum(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putByteArray("picture", byteArray);
params.putString("caption",caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
private void uploadImageToWallAndAlbums(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", byteArray);
params.putString("caption", caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
Add this class on your code
public class SampleUploadListener implements RequestListener{
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onMalformedURLException *******************");
}
#Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onIOException *******************");
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFileNotFoundException *******************");
}
#Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFacebookError *******************");
}
#Override
public void onComplete(String response, Object state) {
Log.d(TAG, "******************* FACEBOOK::onComplete *******************");
}
}

Android how to post picture to friend's wall with facebook android sdk

We can post to facebook friend's wall a text message, but how can we post an image, a picture to a friend's wall using Android Facebook SDK?
When I print out the wall variable it does show correctly USER_ID/feed. After posting the onComplete function of the RequestListener does get called, but there is nothing posted to the friends wall.
Here's example code we're trying to use:
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putString("caption", photoCaption.getText().toString());
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
String wall = null;
wall = fArray.getJSONObject(pos).getString("id").toString() + "/feed";
mAsyncRunner.request(wall, params,"POST", new RequestListener(){
public void onComplete(String response, Object state) {
Log.d("text","facebook post complete");
}
public void onIOException(IOException e, Object state) {
Log.d("text","facebook post onIOException");
}
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.d("text","facebook post onFileNotFoundException");
}
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.d("text","facebook post onMalformedURLException");
}
public void onFacebookError(FacebookError e, Object state) {
Log.d("text","facebook post error");
}
}, null);
This is how I get the list of friends:
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/friends", new RequestListener(){
public void onComplete(String response,Object state) {
try {
jObject = new JSONObject(response);
fArray = jObject.getJSONArray("data");
This is the method i use to post a picture to a wall, it posts a pic from a URL but you can change it to put a byte[] for the pic instead. The message appears above the picture and the caption appears to the right of the picture.
protected void postPicToWall(String userID, String msg, String caption, String picURL){
try {
if (isSession()) {
String response = mFacebook.request((userID == null) ? "me" : userID);
Bundle params = new Bundle();
params.putString("message", msg);
params.putString("caption", caption);
params.putString("picture", picURL);
response = mFacebook.request(((userID == null) ? "me" : userID) + "/feed", params, "POST");
Log.d("Tests",response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}catch(Exception e){
e.printStackTrace();
}
}
EDIT:
To post a byte[] rather than a url to a pic then replace the line
params.putString("picture", picURL); with
params.putByteArray("picture", getIntent().getExtras().getByteArray("data"));
where data is your array.
Using bundle method.
Bundle params = new Bundle();
params.putString("message", "Test Post from karthick");
params.putString("caption", "Karthick kumar");
params.putString("name", "Hai Dude");
**params.putString("icon", "http://www.facebook.com/images/icons/default_app_icon.gif");**
params.putString("source", link);
And Then Use...
mAsyncRunner.request(wall, params,"POST", new RequestListener());

Categories

Resources