I want to get a list of a users albums names and ids. You need to add the permission users_photos for this.
I created a method called fetchAlbums, and in it I am requesting additional permissions. But by the time the permissions dialog pops up, the rest of the method has already executed (without the needed permissions!).
Whats the best way to do this, add the permissions in the onCreate of the Activity?
private void fetchAlbumsFromFB() {
Session session = Session.getActiveSession();
// make a new async request
Bundle params = new Bundle();
params.putString("fields", "id, name");
new Request(
session,
"/me/albums",
params,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
// use response id to upload photo to that album
//errText.setText("New Album created response: " + response.toString());
//need to get the newly created album ID.
try{
JSONObject graphResponse =response.getGraphObject().getInnerJSONObject();
Toast.makeText(getApplicationContext(), "Albums " + graphResponse.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
albID = null;
}
}
}
).executeAsync();
}
It's better to ask for user_photos permission at the starting point, when the user is authenticating the app the first time.
Since it's not an extended/publish permission, it will be asked along with the basic permissions and user wont bother too; else user will see the permission dialogs again and again that could frustrate him.
I guess, in that case this problem will be solved too.
Related
I'm trying to get the name of a user from his user ID.
on this link they say that we can do it from a simple HTTP request like that:
http://graph.facebook.com/4
But it seems that this method is outdated because i cant get anything because:
"An access token is requir…o request this resource."
Anyway i tried using their documentation about the graph api on Android and i did it like that:
GraphRequestAsyncTask request1 = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/"+id1+"/name",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
name1.setText(request1.toString());
But on the name1 TextView i get a message that tells me
{RequestAsyncTask:
connection:null,requests:[{Request:
accesToken:{AccessToken
token:ACCESS_TOKEN_REMOVED permissions:
[public_profile]},
graphPath:/100017723671435/name, graphObject: null,
httpMethod:Get,
parameters:Bundle[{}]}]}
I don't really understand how to do, i have a bunch of facebook ID's on my database and i just want to get the name from the ID, to display it on the screen.
The current user of the app is not the profile which i want to get the infos!
EDIT: It seems i misunderstood the /*handle the result*/ commentary, i made this line like that:
name1.setText(response.toString());
But i have still an error on my TextView:
{Response:
responseCode:400,
graphObject:null,
error: { HttpStatus:400,
errorCode: 2500,
errorType: OAuthException,
errorMessage: Unknown path components: /first_name }}
So it seems i don't use the Graph Paths properly. I'm still looking on the doc and on Google, if i find the answer i'll give the code!
Ensure that you are logged in with read permission.
If you are logged in you can try with:
GraphRequest.Callback gCallback = new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.d(TAG, "onCompleted: ");
if (response != null && response.getJSONObject() != null && response.getJSONObject().has("first_name"))
{
try {
name1.setText(response.getJSONObject().getString("first_name"));
} catch (JSONException e) {
Log.e(TAG, "onCompleted: ",e );
}
}
}
};
new GraphRequest(AccessToken.getCurrentAccessToken(),"/me?fields=id,name,gender,email,first_name,last_name", null,HttpMethod.GET, gCallback).executeAsync();
If you have a problem with login, maybe this link can help you.
I want to share an image on a Facebook page of mine. But I couldn't figure out how to do this step by step. And couldn't find step by step guide for this.
This is my code to share an image on a page
Bundle params = new Bundle();
String nameText = name.getText().toString();
String tags = engine.implodeTags(tagsList);
String textText = text.getText().toString();
params.putString("caption", nameText + "\n\n" + textText + "\n\n" + tags);
params.putString("url", imagesList.get(mainImageSelected).getImageUrl());
params.putString("access_token", "{access token here}");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{page_id here}/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
if(response.getJSONObject()!=null) {
Log.d("qwe", response.getJSONObject().toString());
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(activity, "Shared on facebook", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
else{
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(activity, "Error", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
}
).executeAsync();
It works if I put access token by hand. I am getting access token here
https://developers.facebook.com/tools/explorer .
But after some time this access token is not working any more. So I need to get new access token.
How to get PAGE access token from android itself? Via user login button of facebook sdk?
https://developers.facebook.com/docs/facebook-login/access-tokens
Please help.
Easily if you use facebook SDK to do that. read at here https://developers.facebook.com/docs/sharing/android
You need to get Permanent access token to share images on your Page. To get that you need to follow steps written here.
facebook: permanent Page Access Token?
In an Android App I have the following code:
private void requestDataFromFb() {
Callback callback = new Callback() {
#Override
public void onCompleted(Response response) {
AppLog.Log(TAG, "Facebook Response ::" + response + "");
Utility.closeprocess(Login.this);
loadImage(response);
}
};
String graphPath = "me";
Bundle bundle = new Bundle();
bundle.putString("fields",
"id,picture.height(250),gender,first_name,last_name,age_range");
Request mRequest = new Request(Session.getActiveSession(), graphPath,
bundle, HttpMethod.GET, callback);
RequestAsyncTask task = Request.executeBatchAsync(mRequest);
if (task == null) {
AppLog.Log(TAG, "task is null");
} else {
AppLog.Log(TAG, task.getStatus() + "");
}
}
It's been working fine and I am getting the paths to the user's profile picture. However, after some time the paths don't work anymore. One cannot see the picture of the first users. When I enter the URL in my browser I get:
An error occurred while processing your request.
Reference #50.3d1431b5.1424197137.a584ec67
And when using the Graph API Explorer of Facebook I get a different URL for the picture.
I do not know if that has something to do with the fact that the user has changed their profile picture or something else, and how I could solve this issue.
Any advise or guidance would be greatly appreciated
I am creating an application. I want to access the friends list form Facebook in my app, but i am unable to access the Facebook friends list in my app. All others thinks are accessible, only friend list is not accessible. I have an old app id in which i access all the friends but in my new app id i am unable to access the friend list. In my app, I mention the following permissions:
1. Public_Profiles
2. basic_info
3. user_photos
4. user friends
5. read_friendslist
if any others permissions are needed then please let me know.
You new app is using API v2.0 and in API v2.0 you can not get all friends. /me/friends will only return friends that are also using the app.
There are some new APIs added in v2.0 that you may be able to use: taggable_friends, invitable_friends and social context.
I am getting friends list in two ways:
`Session activeSession = Session.getActiveSession();
if (activeSession.getState().isOpened()) {
Request friendRequest = Request.newMyFriendsRequest(activeSession,
new GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users,
Response response) {
UsefullData.Log("Response : " + response.toString());
UsefullData.Log("Friends list" + users);
//setUserFriendsList(users);
}
});
Bundle params = new Bundle();
params.putString("fields", "id, name, picture");
friendRequest.setParameters(params);
friendRequest.executeAsync();
}`
Request friendRequest = new Request(
activeSession,
"/me/friends",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
UsefullData.Log("Friends Response : " + response.toString());
//setUserFriendsList(response);
}
}
);friendRequest.executeAsync();
Note: set custome permission "user_friends" in your session.
both codes are working at my end. I hope it will help for you.
I am using the Facebook Android SDK. Is there an easy way to get a user's friends who have downloaded the app? For example, the app Draw Something implements this. I can't seem to find any information on this subject
I would guess that if it was possible, some extra information would be needed in the httppost to access this information.
* Note: since 3.14 version, me/friends will only return friends that also use the app, so below implementation is deprecated. See the new "Invitable Friends" or "Taggable Friends" APIs for alternatives.
Using the new Facebook SDK for Android (3.0) is very easy to get your user's friend list.
Following is the code:
private void requestFacebookFriends(Session session) {
Request.executeMyFriendsRequestAsync(session,
new Request.GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users,
Response response) {
// TODO: your code for friends here!
}
});
}
Nevertheless, in order to get the user's friends who are using your Facebook app is a little bit complicated (due to Facebook API documentation). But yes, it is possible.
First of all, create your request:
private Request createRequest(Session session) {
Request request = Request.newGraphPathRequest(session, "me/friends", null);
Set<String> fields = new HashSet<String>();
String[] requiredFields = new String[] { "id", "name", "picture",
"installed" };
fields.addAll(Arrays.asList(requiredFields));
Bundle parameters = request.getParameters();
parameters.putString("fields", TextUtils.join(",", fields));
request.setParameters(parameters);
return request;
}
Note that you need to insert the field "installed" in your request. I'm requesting the user picture path with the same request. Check your possibilities here.
Next, you can use above code to create your request and then get your friends:
private void requestMyAppFacebookFriends(Session session) {
Request friendsRequest = createRequest(session);
friendsRequest.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
List<GraphUser> friends = getResults(response);
// TODO: your code here
}
});
friendsRequest.executeAsync();
}
Note that using this generic request, you don't receive a GraphUser list as response. You'll need following code to get the response as GraphUser list:
private List<GraphUser> getResults(Response response) {
GraphMultiResult multiResult = response
.getGraphObjectAs(GraphMultiResult.class);
GraphObjectList<GraphObject> data = multiResult.getData();
return data.castToListOf(GraphUser.class);
}
Now you can use your user's friend list, with the information if each of your friends use your Facebook app:
GraphUser user = friends.get(0);
boolean installed = false;
if(user.getProperty("installed") != null)
installed = (Boolean) user.getProperty("installed");
I implemented it in this way
Bundle params = new Bundle();
params.putString("fields", "name, picture, location, installed");
And during displaying of the items on the list,in the getView() method i did this
boolean b = jsonObject.getBoolean("installed");
if (b){
Log.d("VIVEK",jsonObject.getString("name"));
}
I hope this post helps you:
How can i get my friends using my facebook App with graph api asp.net
According to the Facebook SDK, the has_added_app field is deprecated and you should use the is_app_user