Above is my code but i cant find out together usage, always gives error
Cannot pass a publish or manage permission (publish_actions) to a request for read authorization
This is my Permission list
private Collection<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
permissions.add("user_birthday");
permissions.add("publish_actions");
And this is login request
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, permissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
} else {
}
}
});
How can i use this together?
After long hours, this is solution. You must behave twice login to facebook. Once is publish and other one is read permissions. If you need public profile data , just publish permission is enough but in my case i need birthday, email, etc.. So code is below;
These are my permissions lists;
Collection<String> readPermissions = new ArrayList<>();
readPermissions.add("public_profile");
readPermissions.add("email");
readPermissions.add("user_birthday");
Collection<String> publishPermissions = new ArrayList<>();
publishPermissions.add("publish_actions");
Firstly, I should login with readpermission
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, readPermissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
listener.onFailure(new UserCancelledFacebookLogin());
} else {
getPublishPermissions(parseUser);
}
}
});
After this, here my "getPublishPermissions" method; FacebookRequestListener is my own listener , don't care/mind delete it.
public void getPublishPermissions(final ParseUser parseUser) {
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// User succesfully login with all permissions
// After this with these json and ParseUser , you can save your user to Parse
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException facebookException) {
}
});
LoginManager.getInstance().logInWithPublishPermissions(activity, publishPermissions);
}
that's all folks =)
happy coding to everyone
The error message means you should not request read and write permissions at the same time. Login with read permissions when the User enters your App, request write permission (publish_actions) only right before you post.
Use ParseFacebookUtils.logInWithPublishPermissionsInBackground for that.
That error message is already well known, take a look at some other thread about it:
How to set permission "publish_actions" in LoginButton using facebook sdk?
Facebook, setReadPermissions and setPublishPermissions
facebook login Cannot pass a publish or manage permission (email) to a request for read authorization
You need to do a POST request instead of a GET one. See:
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/scores/#publish
Sample:
Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());
this is your answer check link
Related
I want to publish post to user's wall. I know Facebook only allows tester and developers to post on wall. I have already added user to tester list. When I try to get publish permission, it says that user has already granted permission (as shown in screenshot) and returns. I am not able to get permission or post on wall. Moreover, callback's any method is not called as well.
CODE
I have followed code from Facebook Example RPSSample.
//Publish to wall
public void publishResult() {
registerPublishPermissionCallback();
if (canPublish()) { //see definition below
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(urlToPost))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
callback.didShareOnFacebookSuccessfully();
}
#Override
public void onCancel() {
// This should not happen
}
#Override
public void onError(FacebookException error) {
showToast(error.getMessage());
}
});
}
}
//check if user has permission or not
private boolean canPublish() {
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
// if we already have publish permissions, then go ahead and publish
return true;
} else {
// otherwise we ask the user if they'd like to publish to facebook
new AlertDialog.Builder(activity)
.setTitle(R.string.share_with_friends_title)
.setMessage(urlToPost)
.setPositiveButton(R.string.share_with_friends_yes, canPublishClickListener)
.setNegativeButton(R.string.share_with_friends_no, dontPublishClickListener)
.show();
return false;
}
}
return false;
}
//If user allows, ask Facebook to grant publish_action permission
private DialogInterface.OnClickListener canPublishClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (AccessToken.getCurrentAccessToken() != null) {
// if they choose to publish, then we request for publish permissions
LoginManager.getInstance()
.setDefaultAudience(DefaultAudience.FRIENDS)
.logInWithPublishPermissions(activity,
Arrays.asList(AppConstants.ADDITIONAL_PERMISSIONS));
}
}
};
//Callback - Any of the method doesn't call.
private void registerPublishPermissionCallback() {
LoginManager.getInstance().registerCallback(
callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
publishResult();
} else {
handleError("Not enough permissions to publish");
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
handleError(exception.getMessage());
}
private void handleError(String errorMessage) {
// this means the user did not grant us write permissions, so
// we don't do implicit publishes
showToast(errorMessage);
}
}
);
}
My app is live on console. Please guide what is requirement of Facebook to get publish permission with test user? Thanks.
Use below code to check if permission is Granted or not.
String url = "/" + "(userFbID)" + "/permissions";
new GraphRequest(AccessToken.getCurrentAccessToken(), url, null,
HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject json = new JSONObject(
response.getRawResponse());
JSONArray data = json.getJSONArray("data");
boolean isPermitted = false;
for (int i = 0; i < data.length(); i++) {
if (data.getJSONObject(i)
.getString("permission")
.equals("publish_actions")) {
isPermitted = true;
String status = data.getJSONObject(i)
.getString("status");
if (status.equals("granted")) {
publishResult()
} else {
LoginFacebook();
}
break;
}
}
if (!isPermitted) {
LoginFacebook();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).executeAsync();
replace user's fb id on place of (userFbID).
If permission is not granted then use this function to ask user for that permission.
private void LoginFacebook() {
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this,
Arrays.asList("publish_actions"));
loginManager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
publishResult()
}
#Override
public void onError(FacebookException error) {
}
#Override
public void onCancel() {
}
});
}
Finally able to solve the problem. There was no issue in code. It was some setting / permission issue with Facebook app. These are steps I followed:
Remove the user from Test role section in your Facebook Developer Console.
Revoke App's Access by logging into that user's account. Go to Settings > Apps. Remove the app from there. You need to wait 10-15 min, since it takes time from Facebook side to revoke access completely.
Make your Facebook App in Sandbox mode (you need to do this to perform publish testing).
Login your android app using any Facebook account other than app admin. It should throw error that app is in development mode.
Add user as Tester in the Facebook Console App. That user may need to approve pending request at his side.
Login from that user account. Facebook will now ask you to provide basic information access to the app. After that you can test publishing. You will be able to post on the user's wall successfully.
Hope this helps someone!
I'm trying to get the facebook's user's timeline in my Android app.
Here my code :
mLoginButton.setReadPermissions(Arrays.asList("user_about_me", "user_friends", "user_likes",
"user_photos", "user_relationships", "user_posts",
"user_status"));
// If using in a fragment
mLoginButton.setFragment(this);
// Other app specific specialization
// Callback registration
mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mAccessToken = loginResult.getAccessToken();
for (String permission : loginResult.getRecentlyGrantedPermissions()) {
Log.d(LOG_TAG, "Granted Permission:" + permission);
}
getUserFeed();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
And after the login, I launch this :
private void getUserFeed() {
Bundle params = new Bundle();
params.putInt("limit", 25);
params.putString("fields", "id,name,link,full_picture,message,story,picture,type,place,from,to");
params.putBoolean("summary", true);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/home",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
final JSONArray data = response.getJSONObject().getJSONArray("data");
//currentJson = response.getJSONObject();
} catch (JSONException e) {
Log.e("Error: ", e.toString());
}
}
}
).executeAsync();
}
I have this respond code from Facebook :
Requires extended permission: read_stream
I know this permission is depreceted, I'm using the latest API 2.5.
Do you know if we can continue to get the user's timeline now, if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
Thanks :)
Do you know if we can continue to get the user's timeline now,
No, you can’t.
if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
/me/home was deprecated together with the permission.
/me/feed is what you can get now, and that’s it.
Which posts you can expect to see is listed here: https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed#readperms
I have been using the Facebook SDK for Android a bit now. I have been having trouble where one of my testers could not create an account in my app using Facebook. After some debugging I figured out that for some reason when he tries to login / register the JSON object that contains the information does not come with his email. This only happens for HIS account!
Does anyone know why this could happen?
(I have included a simplified version of my login code so you can see if there is anything wrong with it that might cause it)
List<String> permissionNeeds = Arrays.asList("public_profile", "user_friends", "email");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.d(TAG, object.toString()); // Has no email sometimes!
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("Error with facebook login: %s", e.getMessage()));
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, String.format("Error with facebook login: %s", error.getMessage()));
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(SplashActivity.this,
Arrays.asList("public_profile", "user_friends", "email"));
}
});
If any more information is needed, please tell me I'll be happy to add some.
Thanks!
So turns out that my tester had an inactive email account.
In general you should never be counting on every Facebook user having an email address.
I am doing simple facebook login using latest facebook sdk 4.8 but after login i am getting null value no user details are coming but only i am getting the details if the user is developer and administrator of that Facebook App.
And also i have given facebook permission.
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_likes",
"user_friends", "email", "user_birthday", "user_photos", "user_about_me"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try{
nameTxt.setVisibility(View.VISIBLE);
nameTxt.setText(jsonObject.getString("name"));
}catch(Exception e){
e.printStackTrace();
Log.d("FacebookException", e.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,gender,birthday,email,bio,photos{link}");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
Have you switched your app status to live? You can see status in your app's Dashboard in developers console.
If not, just switch status to live in Status & Review tab. This switch should be switched to 'Yes' position
I'm using the Facebook registerCallBack method to get the current users information. The problem i am having is while trying to retrieve the email address.
// Callback registration
FBLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
final Profile profile = Profile.getCurrentProfile();
if (profile != null) {
}
//Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String email_id = object.getString("email");
String gender = object.getString("gender");
String facebook_id=profile.getId();
String f_name=profile.getFirstName();
String m_name=profile.getMiddleName();
String l_name=profile.getLastName();
String full_name=profile.getName();
String profile_image=profile.getProfilePictureUri(400, 400).toString();
Log.i("facebook_id",facebook_id);
Log.i("f_name",f_name);
Log.i("m_name",m_name);
Log.i("l_name",l_name);
Log.i("full_name",full_name);
Log.i("profile_image",profile_image);
Log.i("gender",gender);
Log.i("email_id",email_id+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday, friends,albums");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
Using this code i get the following log:
org.json.JSONException: No value for email
I have made sure that the email is publicly available in case that was an issue as i had a similar problem with the Google Plus login script.
Based on my comment above, when you get the differents fields:
parameters.putString("fields", "id, name, email, gender, birthday, friends, albums");
you certainly forgot the right permissions. Indeed, you probably set "user_profile" as follows:
loginButton.setReadPermissions("user_profile");
However, when you look at the reference documentation, you can see that "email" is a permission on its own, it's not included in "user_profile".
Therefore you have to set different permissions, included "email" as well, when you initiate the LoginManager (see Add LoginButton section). Instead of getting only one permissions, you can set an array as:
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_profile", "email", "...", "..."));
Thanks to this, you will be able to retrieve the email address.