我想将朋友的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 请尽快推荐我
答案 0 :(得分:0)
也许你的会话没有得到你朋友生日的许可
您可以申请以下许可:
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);
}
}