I'm following the Facebook Android SDK tutorial and I have all of the code leading up to this step in my app. I start the app (testing it on my phone, but does the same in Emulator) and this screen comes up:
Ok, great!
But, once it's done loading, instead of any sort of log-in screen coming up like the example given here:
I just come up with this:
Code:
package com.greatapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("MY_APP_ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
Ok, I deleted the project and started again. This screen from before is doing something, but keeps reloading this loading screen over and over and over again. Almost like it's contacting Facebook's servers for 1000 different things and each one has a loading screen. I don't know what to do. No errors in Logcat.
It works for me.
Is there any error message in logcat?
Do you use Internet permission?
Check these things.
or maybe you have already completed
or add some log in your code for example:
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
Log.d("onComplete",""+values);
}
#Override
public void onFacebookError(FacebookError error) {
Log.d("onFacebookError",""+error);
}
#Override
public void onError(DialogError e) {
Log.d("onError",""+e);
}
#Override
public void onCancel() {
Log.d("onCancel","cancel");
}
});
and see result in your logcat
Just modify your code like this:
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class MyGreatActivity extends Activity {
public static final String[] PERMISSIONS = new String[] {"email", "publish_checkins", "publish_stream","offline_access"};
Facebook facebook = new Facebook("MY_APP_ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this,PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
Facebook.FORCE_DIALOG_AUTH ---> will login facebook forcefully
also you need to call facebook.logout(context) method to logout current method ,before login with new user.
User this method:
public void Logout() throws MalformedURLException, IOException {
facebook.logout(mContext);
}
Related
Im using the simple facebook library, i have this code:
import com.sromku.simple.fb.listeners.OnLoginListener;
import com.sromku.simple.fb.Permission;
import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.SimpleFacebookConfiguration;
public class MainActivity extends Activity {
private SimpleFacebook mSimpleFacebook;
private Button mButtonLogin;
#Override
public void onResume()
{
super.onResume();
mSimpleFacebook = SimpleFacebook.getInstance(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
// login listener
OnLoginListener onLoginListener = new SimpleFacebook.OnLoginListener()
{
#Override
public void onFail(String reason)
{
Log.w(TAG, reason);
}
#Override
public void onException(Throwable throwable)
{
Log.e(TAG, "Bad thing happened", throwable);
}
#Override
public void onThinking()
{
// show progress bar or something to the user while login is happening
Log.i(TAG, "In progress");
}
#Override
public void onLogin()
{
// change the state of the button or do whatever you want
Log.i(TAG, "Logged in");
}
#Override
public void onNotAcceptingPermissions()
{
Log.w(TAG, "User didn't accept read permissions");
}
};
I get one error in the OnLoginListener, it says the "SimpleFacebook.OnLoginListener cannot be resolved", i have imported the OnLoginListener so i dont understand why it is not working?
Why do you write new SimpleFacebook.OnLoginListener()? The class SimpleFacebook doesn't contain OnLoginListener.
You already imported com.sromku.simple.fb.listeners.OnLoginListener and that's why creating the listener should be following:
OnLoginListener onLoginListener = new OnLoginListener()
{
...
}
you need implement this...
OnLoginListener onLoginListener = new OnLoginListener() {
#Override
public void onLogin() {
// change the state of the button or do whatever you want
Log.i("", "Logged in");
}
#Override
public void onNotAcceptingPermissions(Permission.Type type) {
// user didn't accept READ or WRITE permission
Log.w("", String.format("You didn't accept %s permissions", type.name()));
}
#Override
public void onException(Throwable throwable) {
}
#Override
public void onFail(String reason) {
}
#Override
public void onThinking() {
}
I have used facebook sdk 3.5 in android to create custom login button after clicking will get facebook details then send to server and then go to next intent(screen).I have created the code below and using it but the progress dialog takes long time to get to onCompletemethod() in facebook also sometime its timedOut.I have posted the code below please let me know if there is a better way to login than the one below and if I can reduce the time it takes to login facebook .I really appreciate ay help .Thanks in advance.
public class MainActivity extends Activity {
private ProgressDialog progressDialog;
static String email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Wait...");
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.show();
}
});
Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
progressDialog.dismiss();
email= (String) response.getGraphObject().getProperty("email");
Log.d("email", email);
new FetchTask().execute();
}
}
}).executeAsync();
}
}
});
}});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null)
{
Session.getActiveSession().onActivityResult(MainActivity.this, requestCode, resultCode, data);
}
}
I have the same issue. Maybe you found a solution.
I have the problem on Nexus 4 , but not get it on Samsung S3
Sorry I have no as much reputation to leave a comment.
I need to log in to Facebook and get same fields like email, etc. I use the Facebook SDK, and I set my Android key Hash in developers.facebook and set "Configured for Android SSO". In the simulator and some devices the application works fine.
But if the official Facebook application is installed on the device, my application does not work: I push the login button, but I not see a dialog with a web-view were my password and login are asked for. It looks like the problem in Stack Overflow question Using facebook.authorize with the Android SDK does not call onActivityResult or Stack Overflow question Android Facebook API single sign-on?, but I can not understand how to resolve it.
My code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
public void getAccessToken() {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
#Override
public void onAuthSucceed() {
setupAccessToken(facebookConnector.getFacebook().getAccessToken());
}
#Override
public void onAuthFail(String error) {
Toast.makeText(getApplicationContext(), getString(R.string.error_login), Toast.LENGTH_SHORT).show();
}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();
}
facebookConnector code
public class FacebookConnector {
public void login() {
if (!facebook.isSessionValid()) {
facebook.authorize(this.activity, this.permissions, new LoginDialogListener());
}
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
}
}
Please update the below code of your application. It will solve your problem.
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
I had the same problem like you. Finally, I solved using this:
Open Facebook.java provided by the Facebook SDK and then change it like this:
public void authorize(Activity activity, String[] permissions,
int activityCode, final DialogListener listener) {
boolean singleSignOnStarted = false;
mAuthDialogListener = listener;
/*
// Prefer single sign-on, where available.
if (activityCode >= 0) {
singleSignOnStarted = startSingleSignOn(activity, mAppId,
permissions, activityCode);
}
// Otherwise fall back to the traditional dialog.
if (!singleSignOnStarted) {
*/
startDialogAuth(activity, permissions);
// }
}
This is just a wild guess, but instead of this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
Try:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}
Since you're not calling the parent method some things might not work as expected...
private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
Edit your openactivesession function like this
I want to create a facebook login button for my application.
My problem is, that I want to call 'authorize' with an OnClickListener, but eclipse marks the 'authorize' part as an error. It suggests to replace it with authorizeCallback, but it's not the solution.
It seems like I'm just missing something, but don't know what. Any idea please?
This is where the error occurs (facebook.authorize):
LoginButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
facebook.authorize(this, new String[] { "email", "user_birthday" }, new FacebookLoginDialogListener());
}});
}
Rest of the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public class FacebookLoginDialogListener implements DialogListener
{
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
requestUserData();
}
public void onFacebookError(FacebookError error) {}
public void onError(DialogError e) {}
public void onCancel() {}
}
do this way:
facebook.authorize(YourActivity.this, new String[] { "email", "user_birthday" }, new FacebookLoginDialogListener());
You are passing OnClickListener's this reference to the method, You have to pass Activity's Context to the method.
I can connect to my application in Facebook. I am trying to fetch news feed of application in Facebook.
After login it prompts me if it can 'access posts in my news feed', allow or don't allow. If I click on allow then nothing happens. It just goes back to my activity screen. I am new totally.
Why can't I access news feed section of that application in facebook?
The code is given below:
public class MyGreatActivity extends Activity
{
Facebook facebook = new Facebook("115793565149113");
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new String[] {"read_stream" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
What you've done so far is just setting up the connection.
Now you need to make a request to actually get the data you want.
Add something like this to your code.
AsyncFacebookRunner fbData = new AsyncFacebookRunner(facebook);
Bundle params = new Bundle();
// what data should be retrieved
params.putString("fields", "type, link, from, message, picture, name, description, created_time");
// from which feed would you like data
fbData.request("yourappname/feed", params, new FBRequestListener(this));
// class that will handle the data from facebook
public class FBRequestListener implements RequestListener {
.... // check the documentation on which methods you should include
}