我正在尝试获取已登录Facebook的用户电子邮件。
这是我的代码String userId=(String) user.asMap().get("email");
它返回null,我该如何解决?
我读到可能需要设置会话权限?这个许可是什么?我在哪里需要写这个权限?
也许是另一个问题?
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
updateView();
// End of OnCreate Method>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
private void updateView() {
Session session = Session.getActiveSession();
session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));
if (session.isOpened()) {
//button logout string
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogout(); }
});
} else {
//button log in string
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
}
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private void onClickLogout() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if( session.isOpened() ){
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
String userId=(String) user.asMap().get("email");
i.putExtra("epuzzle",userId );
startActivity(i);
}
});
}
updateView();
}
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sale"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<permission
android:name="com.example.sale.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.sale.permission.MAPS_RECEIVE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sale.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Entry for RegisterActivity.class -->
<activity android:name=".RegisterActivity"
android:label="Register New Account"></activity>
<meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
<activity android:label="@string/app_name" android:name="com.facebook.LoginActivity"></activity>
<activity android:name=".ProfileActivity" android:label="Profile page"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDTVRXMTmfe3jZyAlt-87kRgqM8y4TzIto"/>
</application>
</manifest>
答案 0 :(得分:0)
当您使用Facebook类型的API时,需要Internet权限。请按照以下步骤操作
<uses-permission android:name="android.permission.INTERNET" />
。