Facebook SDK 3.0 for Android without Fragments不起作用

时间:2013-03-07 13:16:13

标签: android facebook android-facebook

我想使用Facebook SDK 3.0 for Android来做一些简单的Graph请求。由于我不会使用Fragment,我无法使用Facebook开发人员文档中发布的示例。

我已经设置了一些简单的Activity,但它们没有用。我通过赏金herehere提出了问题。但这并没有真正帮助。

与此同时,我一直在阅读很多关于Facebook SDK的帖子并尝试了几十个小时,但没有成功。最后,我在this blog上找到了一个额外的代码示例。所以我设置了一个完整的Activity,即将所有朋友的家乡打印到LogCat。

但这也行不通。相反,它只询问基本权限(公共信息+朋友列表),然后退出。我究竟做错了什么?设置了Facebook应用程序的密钥哈希,包名和类名,并且没有任何有趣的内容打印到LogCat。

package com.my.package;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;
import com.facebook.*;

public class FBRequest extends Activity {

    private static final String PERMISSION_FRIENDS_HOMETOWN = "friends_hometown";
    private static final String PREFERENCE_ACCESS_TOKEN = "facebookAccessToken";
    private static final String PREFERENCE_EXPIRATION_DATE = "facebookAccessTokenExpires";
    private ProgressDialog mProgress;
    private SharedPreferences mPrefs;

    private void showToast(final String text) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            }
        });
    }

    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        try {
            startFacebookRequest();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session session = Session.getActiveSession();
        if (session != null) {
            session.onActivityResult(this, requestCode, resultCode, data);
        }
    }

    public interface FacebookConnectHandler {
        public void onSuccess();
        public void onFailure();
    }

    public void startFacebookRequest() {
        connectToFacebook(new FacebookConnectHandler() {
            @Override
            public void onSuccess() {
                // safety check
                if (isFinishing()) { return; }

                showProgressDialog("Connecting to Facebook ...");

                // check for publish permissions

                final List<String> permissions_required = Arrays.asList(PERMISSION_FRIENDS_HOMETOWN);

                if (Session.getActiveSession().getPermissions() == null || !Session.getActiveSession().getPermissions().containsAll(permissions_required)) {
                    // need to make a Session.openActiveSessionFromCache(...) call
                    // because of a bug in the Facebook sdk
                    // where a second call to get permissions
                    // won't result in a session callback when the token is updated

                    if (Session.openActiveSessionFromCache(FBRequest.this) == null) {
                        showToast("Could not connect to Facebook! (3)");
                        return;
                    }

                    Session.getActiveSession().addCallback(new Session.StatusCallback() {
                        @Override
                        public void call(Session session, SessionState state, Exception exception) {
                            if (exception != null || state.equals(SessionState.CLOSED) || state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                                // didn't get required permissions

                                session.removeCallback(this);

                                // safety check
                                if (!isFinishing()) {
                                    showToast("Could not connect to Facebook! (4)");
                                }
                            }
                            else if (state.equals(SessionState.OPENED_TOKEN_UPDATED) && session.getPermissions().containsAll(permissions_required)) {
                                // got required permissions

                                session.removeCallback(this);

                                // safety check
                                if (!isFinishing()) {
                                    startFacebookRequest();
                                }
                            }
                        }
                    });

                    Session.getActiveSession().requestNewReadPermissions(new Session.NewPermissionsRequest(FBRequest.this, permissions_required));
                    return;
                }
                startGraphRequest(Session.getActiveSession());
            }


            @Override
            public void onFailure() {
                cancelProgressDialog();
                showToast("Could not connect to Facebook! (1)");
            }
        });
    }

    private void startGraphRequest(Session session) {
        Request.executeGraphPathRequestAsync(session, "me/friends/?access_token="+session.getAccessToken()+"&fields=id,name,hometown&limit=500", new Request.Callback() {
            @Override
            public void onCompleted(Response response) {
                if (response != null) {
                    try {
                        JSONArray jarr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
                        JSONObject entry;
                        StringBuilder backupStr = new StringBuilder();
                        int nExistingEntries = existingEntries.length;
                        for (int i = 0; i < jarr.length(); i++) {
                            entry = jarr.getJSONObject(i);
                            if (!entry.isNull("id") && !entry.isNull("name") && !entry.isNull("hometown")) {
                                System.out.println(entry.getString("hometown"));
                            }
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        showToast("Unexpected error!");
                    }
                }
            }
        });
    }

    private void connectToFacebook(final FacebookConnectHandler handler) {

        // check whether the user already has an active session
        // and try opening it if we do

        // (note: making a Session.openActiveSessionFromCache(...) call
        // instead of simply checking whether the active session is opened
        // because of a bug in the Facebook sdk
        // where successive calls to update a token
        // (requesting additional permissions etc)
        // don't result in a session callback)

        if (Session.getActiveSession() != null && Session.openActiveSessionFromCache(this) != null) {
            handler.onSuccess();
            return;
        }

        // initialise the session status callback

        Session.StatusCallback callback = new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {

                if (isFinishing()) { return; }

                // check session state

                if (state.equals(SessionState.CLOSED) || state.equals(SessionState.CLOSED_LOGIN_FAILED)) {

                    clearFacebookSharedPreferences();

                    // specific action for when the session is closed
                    // because an open-session request failed
                    if (state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                        cancelProgressDialog();
                        handler.onFailure();
                    }
                }
                else if (state.equals(SessionState.OPENED)) {
                    cancelProgressDialog();

                    saveFacebookSharedPreferences(session.getAccessToken(), session.getExpirationDate().getTime());

                    showToast("Succeeded connecting to Facebook");

                    handler.onSuccess();
                }
            }
        };

        // make the call to open the session

        showProgressDialog("Connecting to Facebook...");

        if (Session.getActiveSession() == null && mPrefs.contains(PREFERENCE_ACCESS_TOKEN) && mPrefs.contains(PREFERENCE_EXPIRATION_DATE)) {
            // open a session from the access token info
            // saved in the app's shared preferences

            String accessTokenString = mPrefs.getString(PREFERENCE_ACCESS_TOKEN, "");

            Date accessTokenExpires = new Date(mPrefs.getLong(PREFERENCE_EXPIRATION_DATE, 0));

            AccessToken accessToken = AccessToken.createFromExistingAccessToken(accessTokenString, accessTokenExpires, null, null, null);

            Session.openActiveSessionWithAccessToken(this, accessToken, callback);
        }
        else {
            // open a new session, logging in if necessary
            Session.openActiveSession(this, true, callback);
        }
    }

    private void saveFacebookSharedPreferences(final String token, final long expiration) {
        if (mPrefs != null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(PREFERENCE_ACCESS_TOKEN, token);
            editor.putLong(PREFERENCE_EXPIRATION_DATE, expiration);
            editor.commit();
        }
    }

    private void clearFacebookSharedPreferences() {
        if (mPrefs != null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.remove(PREFERENCE_ACCESS_TOKEN);
            editor.remove(PREFERENCE_EXPIRATION_DATE);
            editor.commit();
        }
    }

    private void showProgressDialog(final String text) {
        mProgress = ProgressDialog.show(getApplicationContext(), "Facebook", text, true, false);
    }

    private void cancelProgressDialog() {
        if (mProgress != null) {
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
            mProgress = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelProgressDialog();
    }

}

提前致谢!

修改:代码 有效,但我没有致电setContentView(),而Activity的{​​{1}}被调用了在一个错误的情况下。这就是为什么人们无法看到(扩展)权限的第二个请求。

2 个答案:

答案 0 :(得分:9)

上面的代码工作正常,除了我做的小改动,即在显示进度对话框时,为此我已更改上下文给出异常窗口坏标记。我还评论了一条未使用'existingEntries'的行。

我能够登录和应用程序请求访问朋友家乡访问权限。我可以看到朋友ids和他们的家乡。

更新后的工作版本:

package com.example.options;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.Builder;
import com.facebook.Session.NewPermissionsRequest;
import com.facebook.Session.OpenRequest;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;

public class FBRequest extends Activity {

    private static final String PERMISSION_FRIENDS_HOMETOWN = "friends_hometown";
    private static final String PREFERENCE_ACCESS_TOKEN = "facebookAccessToken";
    private static final String PREFERENCE_EXPIRATION_DATE = "facebookAccessTokenExpires";
    private ProgressDialog mProgress;
    private SharedPreferences mPrefs;

    private void showToast(final String text) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            }
        });
    }

    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        try {
            startFacebookRequest();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK && requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE)
        {
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
            Session session = Session.getActiveSession(); 

            if (session != null && !session.isClosed()) 
            {
                startFacebookRequest();
            }
        }
    }

    public interface FacebookConnectHandler {
        public void onSuccess();
        public void onFailure();
    }

    public void startFacebookRequest() {
        connectToFacebook(new FacebookConnectHandler() {
            @Override
            public void onSuccess() {
                // safety check
                if (isFinishing()) { return; }

                showProgressDialog("Connecting to Facebook ...");

                // check for publish permissions

                final List<String> permissions_required = Arrays.asList(PERMISSION_FRIENDS_HOMETOWN);

                if (Session.getActiveSession().getPermissions() == null || !Session.getActiveSession().getPermissions().containsAll(permissions_required)) {
                    // need to make a Session.openActiveSessionFromCache(...) call
                    // because of a bug in the Facebook sdk
                    // where a second call to get permissions
                    // won't result in a session callback when the token is updated

                    if (Session.openActiveSessionFromCache(FBRequest.this) == null) {
                        showToast("Could not connect to Facebook! (3)");
                        return;
                    }

                    Session.getActiveSession().addCallback(new Session.StatusCallback() {
                        @Override
                        public void call(Session session, SessionState state, Exception exception) {
                            if (exception != null || state.equals(SessionState.CLOSED) || state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                                // didn't get required permissions

                                session.removeCallback(this);

                                // safety check
                                if (!isFinishing()) {
                                    showToast("Could not connect to Facebook! (4)");
                                }
                            }
                            else if (state.equals(SessionState.OPENED_TOKEN_UPDATED) && session.getPermissions().containsAll(permissions_required)) {
                                // got required permissions

                                session.removeCallback(this);

                                // safety check
                                if (!isFinishing()) {
                                    startFacebookRequest();
                                }
                            }
                        }
                    });

                    NewPermissionsRequest req = new Session.NewPermissionsRequest(FBRequest.this, permissions_required);
                    req.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
                    Session.getActiveSession().requestNewReadPermissions(req);
                    return;
                }
                startGraphRequest(Session.getActiveSession());
            }


            @Override
            public void onFailure() {
                cancelProgressDialog();
                showToast("Could not connect to Facebook! (1)");
            }
        });
    }

    private void startGraphRequest(Session session) {
        Request.executeGraphPathRequestAsync(session, "me/friends/?access_token="+session.getAccessToken()+"&fields=id,name,hometown&limit=500", new Request.Callback() 
        {
            @Override
            public void onCompleted(Response response) {
                if (response != null) {
                    try {
                        JSONArray jarr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
                        JSONObject entry;
                        StringBuilder backupStr = new StringBuilder();
//                        int nExistingEntries = existingEntries.length;
                        for (int i = 0; i < jarr.length(); i++) {
                            entry = jarr.getJSONObject(i);
                            if (!entry.isNull("id") && !entry.isNull("name") && !entry.isNull("hometown")) {
                                System.out.println(entry.getString("hometown"));
                            }
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        showToast("Unexpected error!");
                    }

                    showToast("Friends list populated");
                    cancelProgressDialog();
                }
            }
        });
    }

    private void connectToFacebook(final FacebookConnectHandler handler) {

        // check whether the user already has an active session
        // and try opening it if we do

        // (note: making a Session.openActiveSessionFromCache(...) call
        // instead of simply checking whether the active session is opened
        // because of a bug in the Facebook sdk
        // where successive calls to update a token
        // (requesting additional permissions etc)
        // don't result in a session callback)

        if (Session.getActiveSession() != null && Session.openActiveSessionFromCache(this) != null) {
            handler.onSuccess();
            return;
        }

        // initialise the session status callback

        Session.StatusCallback callback = new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {

                if (isFinishing()) { return; }

                // check session state

                if (state.equals(SessionState.CLOSED) || state.equals(SessionState.CLOSED_LOGIN_FAILED)) {

                    clearFacebookSharedPreferences();

                    // specific action for when the session is closed
                    // because an open-session request failed
                    if (state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                        cancelProgressDialog();
                        handler.onFailure();
                    }
                }
                else if (state.equals(SessionState.OPENED)) {
                    cancelProgressDialog();

                    saveFacebookSharedPreferences(session.getAccessToken(), session.getExpirationDate().getTime());

                    showToast("Succeeded connecting to Facebook");

                    handler.onSuccess();
                }
            }
        };

        // make the call to open the session

        showProgressDialog("Connecting to Facebook...");

        if (Session.getActiveSession() == null && mPrefs.contains(PREFERENCE_ACCESS_TOKEN) && mPrefs.contains(PREFERENCE_EXPIRATION_DATE)) {
            // open a session from the access token info
            // saved in the app's shared preferences

            String accessTokenString = mPrefs.getString(PREFERENCE_ACCESS_TOKEN, "");

            Date accessTokenExpires = new Date(mPrefs.getLong(PREFERENCE_EXPIRATION_DATE, 0));

            AccessToken accessToken = AccessToken.createFromExistingAccessToken(accessTokenString, accessTokenExpires, null, null, null);

            Session.openActiveSessionWithAccessToken(this, accessToken, callback);
        }
        else {
            // open a new session, logging in if necessary
            OpenRequest op = new Session.OpenRequest(this);

            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
            permissions.add(PERMISSION_FRIENDS_HOMETOWN);
            op.setPermissions(permissions);

            Session session = new Builder(this).build();
            Session.setActiveSession(session);
            session.openForRead(op);
        }
    }

    private void saveFacebookSharedPreferences(final String token, final long expiration) {
        if (mPrefs != null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(PREFERENCE_ACCESS_TOKEN, token);
            editor.putLong(PREFERENCE_EXPIRATION_DATE, expiration);
            editor.commit();
        }
    }

    private void clearFacebookSharedPreferences() {
        if (mPrefs != null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.remove(PREFERENCE_ACCESS_TOKEN);
            editor.remove(PREFERENCE_EXPIRATION_DATE);
            editor.commit();
        }
    }

    private void showProgressDialog(final String text) {
        cancelProgressDialog();
        mProgress = ProgressDialog.show(FBRequest.this, "Facebook", text, true, false);
    }

    private void cancelProgressDialog() {
        if (mProgress != null) {
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
            mProgress = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelProgressDialog();
    }

}

logcat的:

......
......
03-11 18:57:26.781: I/System.out(6729): {"id":"10555075614xxxx","name":"Khammam"}
03-11 18:57:26.781: I/System.out(6729): {"id":"11042167564xxxx","name":"Warangal, India"}
03-11 18:57:26.789: I/System.out(6729): {"id":"11042167564xxxx","name":"Warangal, India"}
03-11 18:57:26.796: I/System.out(6729): {"id":"13074615695xxxx","name":"Jaggayyapet, India"}
03-11 18:57:26.812: I/System.out(6729): {"id":"12530386417xxxx","name":"Kodada, India"}
03-11 18:57:26.812: I/System.out(6729): {"id":"11047265564xxxx","name":"Vishakhapatnam, Andhra Pradesh, India"}

<强>更新

首次登录时会询问所有权限,并删除了SSO登录,因为我没有使用本机应用程序(使用密钥库散列密钥)配置我的Facebook应用程序。

答案 1 :(得分:-2)

在facebook SDK https://github.com/facebook/facebook-android-sdk/

之后使用

以下是您的要求的代码(从Facebook获取好友列表)

try {

  Facebook mFacebook = new Facebook(Constants.FB_APP_ID);
  AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
  Bundle bundle = new Bundle();
  bundle.putString("fields", "birthday");
  mFacebook.request("me/friends", bundle, new FriendListRequestListener());

} catch(Exception e){
    Log.e(Constants.LOGTAG, " " + CLASSTAG + " Exception = "+e.getMessage());
}

public class FriendListRequestListener extends BaseRequestListener {

    public void onComplete(final String response) {
        _error = null;

        try {
            JSONObject json = Util.parseJson(response);
            final JSONArray friends = json.getJSONArray("data");

            FacebookActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    // Do stuff here with your friends array, 
                    // which is an array of JSONObjects.
                }
            });

        } catch (JSONException e) {
            _error = "JSON Error in response";
        } catch (FacebookError e) {
            _error = "Facebook Error: " + e.getMessage();
        }

        if (_error != null)
        {
            FacebookActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "Error occurred:  " + 
                                    _error, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}

我希望它可以帮助你和...其他。