我试图使用Android SDK中4.7 facebook登入。 我曾尝试以下链接http://www.theappguruz.com/blog/android-facebook-integration-tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
Answer 1:
此代码对我的作品,尝试一下,检查是否正在使用Facebook的SDK 4.7
package com.kushal.facebooklogin;
import java.util.Arrays;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class FacebookLogin extends FragmentActivity
{
private TextView tvfirst_name, tvlast_namee, tvfull_name, tvEmail;
private CallbackManager callbackManager;
LoginButton login_button;
String email,name,first_name,last_name;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.main);
tvfirst_name = (TextView) findViewById(R.id.first_name);
tvlast_namee = (TextView) findViewById(R.id.last_name);
tvfull_name = (TextView) findViewById(R.id.full_name);
tvEmail = (TextView) findViewById(R.id.email);
login_button = (LoginButton) findViewById(R.id.login_button);
login_button.setReadPermissions(Arrays.asList("public_profile","email"));
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
login_button.setVisibility(View.GONE);
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
Log.d("JSON", ""+response.getJSONObject().toString());
try
{
email = object.getString("email");
name = object.getString("name");
first_name = object.optString("first_name");
last_name = object.optString("last_name");
tvEmail.setText(email);
tvfirst_name.setText(first_name);
tvlast_namee.setText(last_name);
tvfull_name.setText(name);
LoginManager.getInstance().logOut();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel()
{
}
@Override
public void onError(FacebookException exception)
{
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
在XML设计如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:gravity="center"
android:orientation="vertical" >
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/full_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
该mainefest文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kushal.facebooklogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".FacebookLogin"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
</application>
</manifest>
Answer 2:
您可以使用Facebook的Android SDK中 。 在这里,您将在已经解释的文件如何建立一个网站登录到您的应用程序。
它说:
Facebook的登录添加到您的应用程序最简单的方法是添加
LoginButton
从SDK。 这是一个自定义视图实现Button
。 您可以使用您的应用程序这个按钮来实现的Facebook登录。添加登录按钮
按钮添加到与完整的类名,com.facebook.widget.LoginButton您的布局XML文件:
<com.facebook.login.widget.LoginButton android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" />
然后将它添加到一个片段设置在UI按钮并更新活动使用您的片段。
您可以自定义登录按钮的属性并注册在你的回调
onCreateView()
方法。你可以自定义属性包括
LoginBehavior
,DefaultAudience
,ToolTipPopup
.Style和权限LoginButton
。 例如:@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.splash, container, false); loginButton = (LoginButton) view.findViewById(R.id.login_button); loginButton.setReadPermissions("user_friends"); // If using in a fragment loginButton.setFragment(this); // Other app specific specialization // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); }
如果使用
LoginButton
的片段,你需要设置按钮上的片段如通过调用setFragment
。然后,您需要调用
FacebookSdk.initialize
初始化SDK,然后调用CallbackManager.Factory.create
创建回调管理器来处理登录响应。 下面是添加在一个片段回调的例子:public class MainActivity extends FragmentActivity { CallbackManager callbackManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); LoginButton loginButton = (LoginButton) view.findViewById(R.id.usersettings_fragment_login_button); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { ... }); }
最后,你应该叫
callbackManager.onActivityResult
到登录结果传递给LoginManager
通过callbackManager
。注册一个回调
到到登录结果做出反应,你需要或者注册一个回调
LoginManager
或LoginButton
。 如果你注册回调LoginButton
,无需注册登录上经理回调。您回调添加到您的活动或片段的
onCreate()
方法:@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this.getApplicationContext()); callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); }
如果登录成功,
LoginResult
参数有新AccessToken
,以及最近授予或拒绝的权限。你并不需要一个
registerCallback
,才能成功登录,您可以选择跟随当前与访问令牌变化AccessTokenTracker
下面描述的类。然后,在
onActivityResult()
转发登录结果向callbackManager
中创建onCreate()
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); }
您用FacebookSDK登录或共享整合每个活动和片段应该转发
onActivityResult
到callbackManager
。要了解更多关于获得额外的权限,请参阅:
管理权限,安卓 , 权限与Facebook登录
Answer 3:
我已经使用Facebook的SDK 4.10.0到登录界面,在我的Android应用程序集成。 教程中,我跟着的是:
Facebook登录集成机器人工作室。
你将能够得到姓,名,电子邮件地址,性别,Facebook的帐号和出生日期从facebbok。
上面的教程还介绍了如何通过视频创建的Facebook开发者控制台应用程序。
Gradle.build
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.demonuts.fblogin"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
mavenCentral()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.facebook.android:facebook-android-sdk:4.10.0'
compile 'com.github.androidquery:androidquery:0.26.9'
}
对于activity_main.xml中的源代码
<?xml version="1.0" encoding="utf-8"?> <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"
tools:context="com.demonuts.fblogin.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/text"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:id="@+id/ivpic"
android:src="@mipmap/ic_launcher"/>
<com.facebook.login.widget.LoginButton
android:id="@+id/btnfb"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
代码MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.androidquery.AQuery;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private AQuery aQuery;
private ImageView ivpic;
private TextView tvdetails;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private LoginButton loginButton;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
// Application code
try {
Log.d("tttttt",object.getString("id"));
String birthday="";
if(object.has("birthday")){
birthday = object.getString("birthday"); // 01/31/1980 format
}
String fnm = object.getString("first_name");
String lnm = object.getString("last_name");
String mail = object.getString("email");
String gender = object.getString("gender");
String fid = object.getString("id");
tvdetails.setText("Name: "+fnm+" "+lnm+" \n"+"Email: "+mail+" \n"+"Gender: "+gender+" \n"+"ID: "+fid+" \n"+"Birth Date: "+birthday);
aQuery.id(ivpic).image("https://graph.facebook.com/" + fid + "/picture?type=large");
//https://graph.facebook.com/143990709444026/picture?type=large
Log.d("aswwww","https://graph.facebook.com/"+fid+"/picture?type=large");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email, gender, birthday, location");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_main);
tvdetails = (TextView) findViewById(R.id.text);
ivpic = (ImageView) findViewById(R.id.ivpic);
loginButton = (LoginButton) findViewById(R.id.btnfb);
aQuery = new AQuery(this);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
loginButton.registerCallback(callbackManager, callback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
}
}
Answer 4:
先去https://developers.facebook.com/ ,登录并选择我的应用程序并创建一个应用程序。
通过一个适当按照给定的指令上。
给包名和packagename.ActivityName,选择使用应用程序名称,然后将其保存。
生成散列键(下载的OpenSSL)和(Java JDK)。 - 对于Windows!
提取的OpenSSL充塞到C:\ OpenSSL的
进入命令提示符设置当前路径为JDK的bin文件夹。
然后使用这个命令:
密钥工具-exportcert -alias androiddebugkey -keystore “C:\用户\粉碎机\ .android \ debug.keystore” | “C:\ OpenSSL的\ BIN \ OpenSSL的” SHA1 -binary | “C:\ OpenSSL的\ BIN \ OpenSSL的” 的base64
请确保您使用的是正确的路径就像18:28:50 etc.Mine是shredder.After使用密码:123456。
粘贴hashKey到必填字段。 然后按照沿....
如果需要使用下面的代码。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign__in); String fb_id =""; String fb_fName =""; String fb_lName =""; String fb_email =""; String EMAIL = "email"; CallbackManager callbackManager = CallbackManager.Factory.create(); final LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(Arrays.asList(EMAIL)); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { String userId = loginResult.getAccessToken().getUserId(); GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { getUserFbUserInfo (object); } }); Bundle parameters = new Bundle(); parameters.putString("fields", "first_name,last_name,email,id"); graphRequest.setParameters(parameters); graphRequest.executeAsync(); Intent it = new Intent(getApplicationContext(), Home_Page.class); it.putExtra("fbLogin", true); startActivity(it); } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); } private void getUserFbUserInfo(JSONObject object) { try { fb_email = object.getString("email"); fb_fName = object.getString("first_name"); fb_lName = object.getString("last_name"); fb_id = object.getString("id"); } catch (JSONException e) { e.printStackTrace(); } } @Override protected void onActivityResult ( int requestCode, int resultCode, Intent data){ callbackManager.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); }
}
Answer 5:
检查这个简单的Facebook登录图书馆:
https://github.com/sromku/android-simple-facebook
这里是链接到我上传的演示简单的Facebook登录与自定义按钮: http://www.demoadda.com/demo/android/login-with-facebook_108
它的实现在Android应用程序的Facebook登录的最简单方法。
您可以添加这样的按钮:
<TextView
android:id="@+id/btnFb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:gravity="center"
android:text="Login with Facebook"
android:textColor="@color/white" />
而在gradle这个文件,你可以添加:
compile 'com.sromku:simple-fb:4.1.1'
请检查。
Answer 6:
FACEBOOK LOGIN STEPBYSTEP
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
printHashKey();
meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
btnFacebook.setReadPermissions("public_profile","email");
btnFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
String facebook_email;
String name;
String profilePicUrl;
private ProfileTracker mProfileTracker;
@Override
public void onSuccess(LoginResult loginResult) {
Log.e("facabook","Step1");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("LoginActivity", response.toString());
// Application code
try {
facebook_email = object.getString("email");
name=object.getString("name");
if (object.has("picture")) {
profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
// set profile image to imageview using Picasso or Native methods
}
} catch (JSONException e) {
e.printStackTrace();
}
if(Profile.getCurrentProfile() == null) {
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
// profile2 is the new profile
Log.e("facebook - profile", profile2.getFirstName());
facebookLogin(facebook_email,name,profilePicUrl,Constant.DUMMY_PASSWORD);
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
} else {
facebookLogin(facebook_email,name,profilePicUrl,Constant.DUMMY_PASSWORD);
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(large)");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e("facebook - onCancel", "cancelled");
}
@Override
public void onError(FacebookException e) {
Log.e("facebook - onError", e.getMessage());
Toast.makeText(SignInActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("Request Code", requestCode + "===========");
if(requestCode==64206)
{
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}