Publishing/Creating a poll from an android app to facebook - android

There is an application in facebook called poll(opinion poll), where you can give a question and some options. After you create the poll, others can start to vote. Can I create the poll in my android app and publish it in facebook? i.e the user will give his facebook login credentials and teh app will create the poll on his behalf. Are there any such API's?

Yes, it is possible. The relevant documentation is here: https://developers.facebook.com/docs/reference/api/question/
To do this on your android app, you need to add our Facebook SDK for Android to your app. Once you do so, to create a question you just need to do the following
Authenticate the user via Facebook to get a valid access token
Request the publish_stream and user_questions permissions
Then you just need the following code to create a question and publish it to Facebook:
Bundle params = new Bundle();
params.putString("question", "Cats or dogs?");
JSONObject questions = new JSONObject();
questions.put("Cat");
questions.put("Dog");
params.putString("options", questions.toString())
mAsyncRunner.request("me/questions", params, "POST", new RequestListener() {
#Override
public void onComplete(Bundle values) {
// success
Log.d("fb", values.toString());
}
#Override
public void onFacebookError(FacebookError e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
#Override
public void onFileNotFoundException(FileNotFoundException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
#Override
public void onIOException(IOException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
#Override
public void onMalformedURLException(MalformedURLException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}

Related

How to get facebook user birthday in android?

I use this code to get Facebook user informations. Most of informations are shown but the fact I need is user-birthday. There is no user-birthday in json array. The source code is here. Getting user information code is here
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);
Log.i("profile",profile+"");
// 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() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#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) {
}
});
}
I need some advices. Please!!!
02-03 21:29:35.700: D/Profile(1350): {"id":"1550015884","name":"Aung Htoon Kyaw","first_name":"Aung","middle_name":"Htoon","last_name":"Kyaw","link":"https:\/\/www.facebook.com\/aung.htoonkyaw","hometown":{"id":"105988982772786","name":"Yangon, Burma"},"location":{"id":"115804185098639","name":"Rangoon, Yangon, Burma"},"work":[{"employer":{"id":"107622942602570","name":"KMMGroup"}}],"favorite_athletes":[{"id":"450149851675417","name":"Gohan Kazuto Uzumaki"},{"id":"339211762793623","name":"Natsu Dragneel"},{"id":"1347550405383513","name":"Instagram Girls"},{"id":"331817313532412","name":"Hinata Hyuga"},{"id":"481643341874600","name":"Erza Scarlet"},{"id":"123163411183595","name":"Sanji"}],"education":[{"school":{"id":"109294362421667","name":"BEST High School"},"year":{"id":"140617569303679","name":"2007"},"type":"High School"},{"school":{"id":"104056766297330","name":"University of Computer Studies, Yangon"},"year":{"id":"141778012509913","name":"2008"},"type":"College","with":[{"id":"1059125424","name":"Nara ChannLynn"}]},{"school":{"id":"106398082730475","name":"UCSY"},"year":{"id":"141778012509913","name":"2008"},"type":"College"}],"gender":"male","email":"waptan22\u0040gmail.com","timezone":6.5,"locale":"en_US","verified":true,"updated_time":"2013-09-12T14:32:54+0000","username":"aung.htoonkyaw"}
This is logcat output.
to get more personal details you need a user permission, so pragmatically you need to add permission to your session and prompt it to the user to accept/deny this permission.
if you are following the Androidhive tutorial, I am afraid it is the old Facebook SKD so you need to read the new version of the SDK, to solve your problem check this page out and specifically Step 2
on snippet of adding permission code:
authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes"));
...

invite friends to facebook app - android, no invitations sent

I have created a facebook event with my android coding and now I want to invite friends to it.
Below is my code to invite friends:
private void inviteFriends()
{
try
{
Bundle params = new Bundle();
params.putString("title", "invite friends");
params.putString("message", "come join us!");
facebook.dialog(this, "apprequests", params, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
public void onComplete(String response, Object state)
{
try
{
JSONObject eventResponse = new JSONObject(response);
//event_id = event.getString("id");
Log.i(TAG, "Event Response => "+eventResponse);
Log.w("myapp", friends);
//Toast.makeText(getApplicationContext(), "New Event Created!!", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
}
}
});
}
catch (Exception e)
{
}
}
The code works up to selecting friends, the title and message comes with the friend list. I can send the invitation but no notifications are sent to selected people.
Please tell me what needs to be done. Thank you in advance!
this image.
Have you turned on the App omn Facebbok Tab in your app developer setting like this.
If don't then it will not sent notification to your friends.

Facebook Android SDK. Error validating access token

I am trying to access user information from facebook sdk.But I keep getting this error.
{"error":{"message":"Error validating access token: The session has been invalidated because the user has changed the password.","type":"OAuthException","code":190,"error_subcode":460}}
Here is the call which returns me the error in the response parameter of the oncomplete function.
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response; //<-- error in response
try {
JSONObject profile = new JSONObject(json);
MainActivity.this.userid = profile.getString("id");
new GetUserProfilePic().execute();
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + MainActivity.this.userid, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
Log.e("jsonexception",e.getMessage());
facebook.extendAccessTokenIfNeeded(MainActivity.this, null);
GetUserInfo();
}
}
#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) {
}
});
Sometimes I get the correct response also.I think this is due to the access token expiration if I am right.
So can you guys tell me how to extend the access token although I've used this facebook.extendAccessTokenIfNeeded(this, null); in the onResume method of the activity.
How to solve this?
Both the extendAccessTokenIfNeeded and extendAccessTokenIfNeeded methods require the native Facebook app to be installed locally.
(I don't know if you are using v2 or v3b of the SDK, but it's documented here https://developers.facebook.com/docs/reference/android/3.0/Facebook#extendAccessToken(Context,%20ServiceListener) )
The behavior you're seeing... is it in the emulator or a real device? If the former, you can install the native Facebook app as an APK from v3b of the SDK, available here: https://developers.facebook.com/android

how to change the facebook profile picture using facebook android sdk?

my english is not so perfect but i want to learn, so i hope you can understand me. I wanna know how can i change the facebook profile pic using facebook android sdk. im able to upload pictures from my app to any album, including "Profile Pictures" (i use the next code:)
params=new Bundle();
params.putByteArray("photo", photo);
params.putString("caption", description);
mAsyncRunner.request(idAlbum+"/photos", params,"POST",new RequestListener() {
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {}
#Override
public void onIOException(IOException e,Object state) {}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state){}
#Override
public void onFacebookError(FacebookError e,Object state) {}
#Override
public void onComplete(String response, Object state) {
mHandler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_LONG).show();
}
});
}
}, null);
but i dont know how to set the uploaded image as the profile pic. maybe adding some code to this? I know that what i ask is posible using facebook sdk for php. also, i have seen an Apple app that do this too. Then, i think i can do it in android. somebody has information about what im looking for? thanks in advance.
Ok, I found the way to do that. and this is the code:
params=new Bundle();
try {
params.putByteArray("photo", photo);
} catch (IOException e) {
e.printStackTrace();
}
params.putString("caption", description);
mAsyncRunner.request(idAlbum+"/photos", params,"POST",
new RequestListener() {
#Override
public void onMalformedURLException(MalformedURLException
e, Object state) {}
#Override
public void onIOException(IOException e,Object state) {}
#Override
public void onFileNotFoundException(
FileNotFoundException e, Object state){}
#Override
public void onFacebookError(FacebookError e,Object state) {}
#Override
public void onComplete(final String response, Object state) {
mHandler.post(new Runnable() {
#Override
public void run() {
try {
JSONObject json=new JSONObject(response);
JSONObject obj=new JSONObject(
facebook.request("me"));
final String photoId=json.getString("id");
String userId=obj.getString("id");
String url="https://m.facebook.com/photo." +
"php?fbid="+photoId+"&id="+userId+
"&prof&__user="+userId;
Intent mIntent=new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(mIntent);
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}, null);
Post the photo on a specific album, get the photoID with a JSONObject, and then redirect to a web page where the user can confirm to set the photo as his/her profile picture.

invalid application id

I am using following code to load an image from android app to facebook. I have installed facebook sdk for android. But the app is not doing the intended. I am getting "invalid application id" in the logcat. What mistake am I making ?
Button mButton=(Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Facebook mFacebook=new Facebook(yourAppID)
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(imageLink);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
mAsyncRunner.request(null, params, "POST",
new SampleUploadListener(), null);
}
});
public class SampleUploadListener extends BaseRequestListener {
#SuppressWarnings("unused")
public void onComplete(final String response, final Object state) {
try {
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
String src = json.getString("src");
PublishImage.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"Successfully Uploaded", Toast.LENGTH_SHORT)
.show();
}
});
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public abstract class BaseRequestListener implements RequestListener {
public void onFacebookError(FacebookError e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onFileNotFoundException(FileNotFoundException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onIOException(IOException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onMalformedURLException(MalformedURLException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
}
Logcat
06-15 20:00:55.398: W/KeyCharacterMap(629): No keyboard for id 0
06-15 20:00:55.398: W/KeyCharacterMap(629): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-15 20:01:02.438: D/dalvikvm(629): GC_EXTERNAL_ALLOC freed 1214K, 59% free 2755K/6599K, external 1995K/2266K, paused 630ms
06-15 20:01:08.998: D/MediaPlayer(629): getMetadata
06-15 20:01:15.529: D/Facebook-Example(629): Response: {"error_code":101,"error_msg":"Invalid application ID.","request_args":[{"key":"method","value":"photos.upload"},{"key":"format","value":"json"}]}
06-15 20:01:15.529: W/Facebook-Example(629): Facebook Error: Invalid application ID.
EDIT: This worked
class ButtonListener1 implements View.OnClickListener{
Facebook facebook=new Facebook(ID);
#Override
public void onClick(View v) {
facebook.authorize(Pic.this, new String[] { "publish_stream" },
new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError dialogError) {
// TODO Auto-generated method stub
}
#Override
public void onComplete(Bundle values) {
postToWall(values.getString(Facebook.TOKEN));
}
private void postToWall(String accessToken) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, accessToken);
bundle.putByteArray("facebookPictureData", data);
// The byte array is the data of a picture.
bundle.putByteArray("picture", getIntent().getExtras().getByteArray("data"));
try {
facebook.request("me/photos", bundle, "POST");
} catch (FileNotFoundException fileNotFoundException) {
// makeToast(fileNotFoundException.getMessage());
} catch (MalformedURLException malformedURLException) {
// makeToast(malformedURLException.getMessage());
} catch (IOException ioException) {
// makeToast(ioException.getMessage());
}
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
}
UPDATE:
Here is the link to the Facebook.java class on facebook api reference website:
http://developers.facebook.com/docs/reference/androidsdk/authentication/. Scroll down a few lines to find the authorize method. Are you sure you are using the com.facebook.android.Facebook class from the android sdk and not a class that you might have spun on your own?
The basic auth flow on android apps is as follows:
1. Make an instance of the Facebook class (that you have).
2. Make a call to Facebook.authorize() with the right permissions you need.
3. In the DialogListener call back of the authorize method, you can save the access_token and expiration time for future use.
4. In the activity that you called authorize from, override the "onActivityResult method and if that method is called with Facebook's result code (Facebook.DEFAULT_AUTH_ACTIVITY_CODE), then call Facebook.authorizeCallback with the received intent.
5. After these 4 steps are done, you are free to make graph api requests with Facebook.request() or AsyncFacebookRunner.request(). In these request methods, the first parameter is the graph path you want. So, for example, if you called the request method with path "me", you would get back profile information of the logged in user.
END UPDATE-------
For starters, I think you necessarily need to have the graph path as the first parameter in your code on this line:
mAsyncRunner.request("*********7618/photos", params, "POST",
new SampleUploadListener(), null);
}
});
In my code it is the albumid/photos. Additionally, you need permissions from the user to upload to an album belonging to her. For that, you need to retrieve an access token using Facebook.authorize().
I am not sure I understand why you are getting the error related to the app id being not valid, however, from what I know this are the basic steps you need to do in order to post photos before you post photos.
I am sure someone will post a better answer soon, hope this helps in the meantime.

Categories

Resources