Facebook SDK 3.0 - Get Facebook User ID and Access Token - android

I searched for past two days and was not successful in finding the method to get the user id and access token from Facebook SDK 3.0 - Native Login .
i am following facebook native login - http://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/
and i get the access token using Session.getAccessToken , i get some access token but that is not valid . what is the actual procedure ? Am i doing wrongly ?
How to get the UserId in Native Login using Facebook SDK 3.0

user id:
final Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// If the session is open, make an API call to get user data
// and define a new callback to handle the response
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
user_ID = user.getId();//user id
profileName = user.getName();//user's profile name
userNameView.setText(user.getName());
}
}
}
});
Request.executeBatchAsync(request);
}
user_ID & profileName are string.
for accessToken:
String token = session.getAccessToken();
EDITED: (13/1/2014)
for user email (i haven't check this code by running on device or emulator):
these are only my opinion or you can call it suggestion
setReadPermissions(Arrays.asList("email", ...other permission...));
//by analyzing the links bellow, i think you can set the permission in loginbutton as:
loginButton.setReadPermissions(Arrays.asList("email", ...other permission...));
user.asMap().get("email");
for more info see:
link1, link2, link3, link4,

Related

The Facebook login doesn't work anymore since I upgraded the Firebase app to the new Console (only)

I had a working app with Facebook & Email Login feature, since I upgrade the Firebase console (only, the sdk has not been update).
The app release before the Firebase 3.0 was working before, but it is not anymore able to sign/log with Facebook after the console has been upgraded.
What I have done:
1 - Upgraded the Firebase console
Because of Firebase & Facebook console update, I also had to put the Oauth Callback to the Facebook App
2 - Pasted the Firebase Facebook OAuth Callback to the Facebook console (before it was void) `https://xxx.firebaseapp.com/__/auth/handler``
The Exception:
The firebase Auth listener trigger a Firebase Error :
Invalid authentication credentials provided. and Facebook :
{"providerErrorInfo":{"code":400,"message":"Unsuccessful debug_token
response from Facebook: {\"error\":{\"message\":\"(#100) You must
provide an app access token or a user access token that is an owner or
developer of the
app\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"DG4lLRJHFBS\"}}"}}
The FirebaseError Code:
In the decompiled code of the FirebaseAndroidSdk, the error object is:
0 = {java.util.LinkedHashMap$LinkedEntry#22680} "code" ->
"INVALID_CREDENTIALS"
1 = {java.util.LinkedHashMap$LinkedEntry#22681}
"message" -> "Invalid authentication credentials provided."
2 = {java.util.LinkedHashMap$LinkedEntry#22682} "details" ->
"{"providerErrorInfo":{"code":400,"message":"Unsuccessful debug_token
response from Facebook: {\"error\":{\"message\":\"(#100) You must
provide an app access token or a user access token that is an owner or
developer of the app\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"BtB3JF2qmku\"}}"}}"
with the decompiled code:
private void makeAuthenticationRequest(String urlPath, Map<String, String> params, AuthResultHandler handler) {
final AuthenticationManager.AuthAttempt attempt = this.newAuthAttempt(handler);
this.makeRequest(urlPath, HttpRequestType.GET, params, Collections.emptyMap(), new RequestHandler() {
public void onResult(Map<String, Object> result) {
Object errorResponse = result.get("error");
String token = (String)Utilities.getOrNull(result, "token", String.class);
if(errorResponse == null && token != null) {
if(!AuthenticationManager.this.attemptHasBeenPreempted(attempt)) {
AuthenticationManager.this.authWithCredential(token, result, attempt);
}
} else {
FirebaseError error = AuthenticationManager.this.decodeErrorResponse(errorResponse);
AuthenticationManager.this.fireAuthErrorIfNotPreempted(error, attempt);
}
}
public void onError(IOException e) {
FirebaseError error = new FirebaseError(-24, "There was an exception while connecting to the authentication server: " + e.getLocalizedMessage());
AuthenticationManager.this.fireAuthErrorIfNotPreempted(error, attempt);
}
});
}
At AuthListener level, the firebaseError code : -20
https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/FirebaseError.html
The specified authentication credentials are invalid.
The Facebook Error Code:
code 400
Nothing relevant found here : https://developers.facebook.com/docs/graph-api/using-graph-api/#errors
The code for Authing:
public void authWithFirebase(final String provider, Map<String, String> options) {
if (options.containsKey(AUTH_OPTIONS_ERROR)) {
EventBus.getDefault().post(new MessageToDisplayEvent(options.get(AUTH_OPTIONS_ERROR), true));
} else {
if (provider.equalsIgnoreCase(AUTH_PROVIDER_TWITTER)) {
// if the provider is twitter, we must pass in additional options, so use the options endpoint
ref.authWithOAuthToken(provider, options, new AuthResultHandler(provider));
} else {
// if the provider is not twitter, we just need to pass in the oauth_token
ref.authWithOAuthToken(provider, options.get(AUTH_OPTIONS_TOKEN), new AuthResultHandler(provider));
}
}
}
TOKEN Validity:
From the code above, the Token is confirmed valid since :
https://graph.facebook.com/app?access_token=%7Byour_access_token%7D return a valid JSON
And the Facebook Tool AccessToken https://developers.facebook.com/tools/debug/accesstoken return a still valid TOKEN
What changed from user point of view:
Now, When I click on the FacebookLoginButton, I have a new dialog that ask "connection as %FacebookUserName", with 2 buttons ("Unconnect" & "Cancel")
I posted a bug report at Firebase, but I even do not know if this is Facebook or Firebase, any help, advise for exploring new issue surface or solution is welcome.
In Facebook Developper Console, switch-off the option about the "app key that is integrated in the client".
For me this changed the behavior. I will give more information as far I get from Firebase/Facebook
Here is a French Screenshot to help you setting up Facebook:

How to retrieve authData from a ParseUser?

I'm trying to retrieve the authData field from a ParseUser. With Parse 1.9.1, I used to do it like so:
ParseUser user = ParseUser.getCurrentUser();
HashMap authDataMap = (HashMap)user.get("authData");
HashMap facebookMap = (HashMap)authDataMap.get("facebook");
String facebookId = (String)facebookMap.get("id");
And this worked fine.
Something changed though. I don't know if it's because I updated to Parse 1.9.2 or if something changed on the Parse server side, but authData is no longer accessible. The line user.get("authData") returns null. Even if I re-fetch the user.
Ultimately I want to retrieve the Facebook id from the ParseUser, preferably without reaching out to Facebook. Is this no longer possible?
If you are using ParseFacebookUtils to perform login Facebook user then after successfully login from in parse try to get GraphUser using following to fetch Facebook user data-
Request.newMeRequest(ParseFacebookUtils.getSession(),
new Request.GraphUserCallback() {
#Override
public void onCompleted(
final GraphUser fbUser,
Response response) {
try {
if (fbUser != null
&& parseUser != null
&& fbUser.getName()
.length() > 0) {
// Facebook user data
String fbId = fbUser.getId();
} else {
// Facebook user not logged in
}
} catch (Exception e) {
e.printStackTrace();
stopLoading();
}
}
}).executeAsync();
Have you taken a look at Facebook Users section in the Parse.com documentation. I think authData is for internal communication, not meant to be called (any more).

Facebook API User Request graphObject is null

So I have an application that is currently using the facebook api to get a users info from facebook. The application has been working 100% until today when suddenly, certain info wouldnt load. I traced it back to the Facebook me request foor getting into from the current user. This is the Log of the request:
{Request: session: {Session state:OPENED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[user_birthday, basic_info]}, appId:349620835240734}, graphPath: me, graphObject: null, httpMethod: GET, parameters: Bundle[{}]}
The graph object is null and the onCompleted method is never called. As I stated before, this has been working great up until now and I have not touched this class at all. The entire request is below, and any help is GREATLY appreciated!
Request request = Request.newMeRequest(mSession, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (mSession == Session.getActiveSession()) {
if (user != null) {
String currentUserId = user.getId();
String name = user.getFirstName();
String birthday = user.getBirthday();
String age = getAge(birthday);
String gender = user.asMap().get(Constants.GENDER).toString();
mParseUser.put(Constants.NAME, name);
mParseUser.put(Constants.AGE, age);
mParseUser.put(Constants.GENDER, gender);
mParseUser.saveInBackground();
SharedPreferences pref =
mContext.getSharedPreferences(ParseUser.getCurrentUser().getUsername(),
Context.MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString(Constants.FACEBOOK_USER_ID, currentUserId);
ed.apply();
}
}
}
});
Request.executeBatchAsync(request);
Log.e("RequestValue", String.valueOf(request));
Use this library when you deal with Facebook API.because it is AWESOME!! Really!..
https://github.com/sromku/android-simple-facebook
Read the docs and enjoy the library.
Tell me if it helps

Create new ParseUser using Google+ AccessToken returns InvalidSession?

I'm trying to create a new ParseUser using a Google+ SignIn. While I'm able to retrieve the access token successfully from Google, I get a ParseException(InvalidSession).
I'll post a few snippets that are relevant.
This is how im getting the AccessToken from Google
final String SCOPES = "https://www.googleapis.com/auth/plus.login ";
token = GoogleAuthUtil.getToken(
MainActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + SCOPES);
Making ParseUser
ParseUser.becomeInBackground(token, new LogInCallback()
{
public void done(ParseUser user, ParseException e)
{
Log.i(TAG, "makeParseUser"+"2");
if (user != null)
{
// The current user is now set to user.
/*
user.put("name", s1);
user.put("email",s6);
user.saveInBackground();
*/
}else
{
// The token could not be validated.
Log.i(TAG, "makeParseUser"+e.getLocalizedMessage());
}
}
});
A similar question has been asked here but there doesn't seem to be a proper solution to it.
Currently Parse doesn't support G+ login. Practically it can be done by using Parse cloud code.
ParseUser.becomeInBackground(); expects Parse User token, but not G+ one.

Android - Facebook Connect, Check server side

I was wondering how can I check if the user is connected to facebook on server side if connection comes from an Android APP.
Indeed, on my website I can easily check that in PHP when the request comes from a browser with :
// Try to get the user's id on Facebook
$userId = $this->facebook->getUser();
// If user is not yet authenticated, the id will be zero
if($userId == 0){
// Generate a login url
$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'email'));
$response = array('status' => 'failed', 'error' => 'facebook_not_authentificated', 'url' => $data['url']);
$data = json_encode($response);
echo $data;
return 0;
}
But when I call the url in my Android App, I do not pass this step and I get the JSON response {'status' => 'failed', 'error' => 'facebook_not_authentificated', 'url' => '...' }
Any idea ?
Thanks
If you want to check if the user still authenticated in the device, you should use Facebook Android SDK on the device, not use the server side.
On the other hand, you can send the token you got from Facebook and send it to your server and check if it gives you a Facebook User.
If someone need the answer i finally found out after many hours of research ...
You have to pass the token (token that you get into your android app), to the server and tell the server to use it :
$facebook->setAccessToken($access_token);
To find out the token in your app :
Session.openActiveSession(InscriptionConnexionActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
System.out.println(user.getName() + " : connected");
$my_token = session.getAccessToken());
}
}
});
}
}
});

Categories

Resources