如何在Android中使用facebook连接获取我的电子邮件地址和Facebook ID?
答案 0 :(得分:1)
您需要电子邮件权限才能阅读用户的电子邮件地址。那 将在[uid]或我的请求中为你的json-response添加一个电子邮件标签。
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.LoginButton;
public class FacebookIntegrationActivity extends Activity
{
public static final String APP_ID = "166597773469222"; // TestFBIntegration App
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private LoginButton mLoginButton;
private TextView mText;
private Button buttonLoad = null,buttonBlackAndWhite=null;
private String uniq_id,first_name,last_name,avatar,user_name,emailaddress;
private static final String[] PERMISSIONS =
new String[] {"publish_stream", "read_stream", "offline_access","user_photos","user_likes","friends_photos","friends_likes","email"};
private ImageView imageViewFacebook = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
try
{
if (APP_ID == null)
{
Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " +
"specified before running this example");
}
mLoginButton = (LoginButton) findViewById(R.id.login);
mText = (TextView) findViewById(R.id.txt);
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
mLoginButton.init(this, mFacebook,PERMISSIONS);
buttonLoad = (Button)findViewById(R.id.buttonLoad);
buttonLoad.setOnClickListener(this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public class SampleAuthListener implements AuthListener
{
public void onAuthSucceed()
{
mText.setText("You have logged in! ");
mAsyncRunner.request("me&fields=id,name,email,first_name,last_name,picture", new SampleRequestListenerForUniqueId());
Log.i("------------- Get Access Tokens","=============== :: "+mFacebook.getAccessToken());
}
public void onAuthFail(String error) {
mText.setText("Login Failed: " + error);
}
}
public class SampleLogoutListener implements LogoutListener
{
public void onLogoutBegin()
{
mText.setText("Logging out...");
}
public void onLogoutFinish()
{
mText.setText("You have logged out! ");
}
}
public class SampleRequestListenerForUniqueId extends BaseRequestListener
{
public void onComplete(final String response,Object state)
{
try
{
JSONObject dataObject = Util.parseJson(response);
uniq_id = dataObject.getString("id");
user_name = dataObject.getString("name");
first_name = dataObject.getString("first_name");
last_name = dataObject.getString("last_name");
emailaddress = dataObject.getString("email");
avatar = dataObject.getString("picture");
}
catch (JSONException e)
{
e.printStackTrace();
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/white"
android:gravity="center_horizontal">
<com.facebook.android.LoginButton
android:id="@+id/login"
android:src="@drawable/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:visibility="gone"/>
<TextView android:id="@+id/txt"
android:text="@string/hello"
android:textColor="@drawable/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Button
android:id="@+id/buttonLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Photo Albums"
android:visibility="gone"/>
<ImageView
android:id="@+id/imageViewFacebook"
android:layout_width="300dip"
android:layout_height="300dip"
android:src="@drawable/login" />
<Button
android:id="@+id/buttonBlackAndWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black and White"/>
</LinearLayout>
答案 1 :(得分:0)
我不确定,但如果我没弄错,可以从Facebook提供的Graph API获取此信息。您需要登录才能访问它。
答案 2 :(得分:0)
LoginButton是我在视图中实现的Facebook SDK本机对象(com.facebook.widget.LoginButton)。
查看代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/login_options"
xmlns:android="http://schemas.android.com/apk/res/android">
<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">
</com.facebook.widget.LoginButton>
</RelativeLayout>
Java代码:
RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.login_options, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("email"));