正在将facebook生日列表导入我的android应用程序



我想将朋友的facebook生日导入我的应用程序。目前,我的应用程序能够创建新的生日活动。但添加每个生日都需要很长的过程,所以我想要一个代码来将所有facebook朋友的生日导入我的应用程序。我使用过facebook会话,但到现在为止没有得到太多

facebook = new Facebook(Constants.FB_APP_ID);
    Session session = new Session(getApplicationContext());
    if (session.isOpened()) {
        Toast.makeText(getApplicationContext(), session.getState().toString(),
                Toast.LENGTH_LONG).show();
    } else
        Toast.makeText(getApplicationContext(), session.getState().toString(),
                Toast.LENGTH_LONG).show();
    fbRunner = new AsyncFacebookRunner(facebook);

此外,我已经创建了一个请求

private void importBirthdayFromFB() {
    Toast.makeText(getApplicationContext(), "Clicked on fb button",
            Toast.LENGTH_LONG).show();
    fbRunner.request("maxparera/friends", new RequestListener() {
        @SuppressWarnings("unchecked")
        @Override
        public void onComplete(String response, Object state) {
            String keys = "";
            try {
                JSONObject profile = new JSONObject(response);
                // getting name of the user
                Iterator<String> keysIterator = profile.keys();
                if (keysIterator != null) {
                    while (keysIterator.hasNext()) {
                        keys += keysIterator.next().toString();
                    }
    Toast.makeText(getApplicationContext(), keys, Toast.LENGTH_LONG).show();
                }
                final String name = profile.getString("name");
                // getting name of the user
    Toast.makeText(getApplicationContext(), Name: " + name, Toast.LENGTH_LONG).show();
            } catch (final JSONException e) {
                e.printStackTrace();
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    });

但我没有得到任何名字,而是得到了异常,满足了OAuthException请建议我尽快

也许你的会话没有权限为你的朋友庆祝生日
您可以请求如下权限:

LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_friends, read_friendlists));

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        />
</LinearLayout>

注意:我使用的是Facebook SDK 3.8,你可以参考它的示例了解更多细节
LoginButton是Facebook SDK的一个组件。

对于我的全部代码:

public class MainFragment extends Fragment {
    private static final String TAG = "MainFragment";
    private UiLifecycleHelper uiHelper;
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
            Log.i(TAG, "session: " +session.toString());
            Log.i(TAG, "state: " +state.toString());
            if(exception!=null)
                Log.i(TAG, "exception: " +exception.toString());
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);
//REQUEST PERMISSION
        authButton.setReadPermissions(Arrays.asList("user_friends, read_friendlists));
        return view;
    }
    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            try{
                Log.i(TAG, "Logged in...");
                Log.i(TAG, "Execute my request");
                Request r = new Request(session,"/me/friendlists",null,HttpMethod.GET,new Request.Callback() {
                            public void onCompleted(Response response) {
                                Log.i(TAG, "onCompleted");
                                try {
//I HAD FRIEND LIST
                                    Log.i(TAG, "FRIEND: " + response.getGraphObject().toString());
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    );
                r.executeAsync();
                Log.i(TAG, "Finish my request");
            }catch (Exception ex){
                Log.e(TAG, ex.getMessage());
            }
        } else if (state.isClosed()) {
            try{
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
            Log.i(TAG, "Logged out...");
        }
    }
    @Override
    public void onResume() {
        super.onResume();
     // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null &&
               (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }
        uiHelper.onResume();
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}

相关内容

最新更新