我想在我的Android应用中添加登录facebook功能。我怎样才能做到这一点? 是对的吗? https://developers.facebook.com/docs/facebook-login/android
答案 0 :(得分:11)
第一步
您注册了自己的应用吗?如果您没有,请转到https://developers.facebook.com/apps/,点击右上角的添加新应用并按照说明操作。
第二步
第二步是将Facebook SDK添加到您的项目。您可以按照https://developers.facebook.com/docs/android/getting-started
进行操作在Android Studio中,使用API 15作为最低SDK
Facebook SDK可在Maven Central上使用,因此请修改build.gradle
并在repositories { mavenCentral() }
之前添加dependencies
然后,在compile 'com.facebook.android:facebook-android-sdk:4.7.0
dependencies
构建项目并将Facebook SDK初始化到您的应用程序
代码如下:
import com.facebook.FacebookSdk;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
第三步
您在注册应用时收到的Facebook应用ID应添加到strings.xml
。
<string name="facebook_app_id">123456908761030</string>
你必须编辑你的AndroidManifest.xml
添加一个INTERNET uses-permission
(能够连接到Facebook的服务器)和一个meta-data
添加到Application
元素(用于Facebook App ID)。
<uses-permission android:name="android.permission.INTERNET"/>
<application android:label="@string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
</application>
第四步
将FacebookActivity
定义为您的清单中的另一个活动。如果处理大多数配置使用configChanges
自行更改。
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
第五步
在XML布局中创建/添加登录按钮(无论您想要的位置)。
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
第六步
在您的活动中,将您在布局中定义的小部件声明为类的字段。
private LoginButton loginButton;
将CallbackManager
声明为另一个字段来管理应用中使用的回调。
private CallbackManager callbackManager;
初始化您的CallbackManager
。
callbackManager = CallbackManager.Factory.create();
使用findViewById
初始化窗口小部件。
loginButton = (LoginButton) view.findViewById(R.id.usersettings_fragment_login_button);
最后一步
创建一个回调来处理登录结果:
如果登录尝试成功,则调用onSuccess
如果用户取消登录尝试,则调用onCancel
如果发生错误,则调用onError
代码是:
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 e) {
// App code
}
});
然后,在onActivityResult()
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
有些链接
TutsTlus教程:LINK
应用示例(slidenerd):LINK
视频教程(slidenerd):LINK
获取用户信息:LINK
答案 1 :(得分:1)
**Process Login With Facebook**
**Working on Play Store**
**Link
- https://play.google.com/store/apps/details?id=com.codecube.airbucks&hl=en
**
Registered your new app? If you didn't, go to https://developers.facebook.com/apps/, click Add a New App in the top right and follow the instructions. **step by step**
**Manifest Permission**
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
**build.Gradle Dependency**
compile 'com.facebook.android:facebook-android-sdk:4.8.0' // in airbuck
/*compile 'com.facebook.android:facebook-android-sdk:[4,5)'*/
<activity android:name="com.facebook.FacebookActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationName"
android:value="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<provider android:authorities="com.facebook.app.FacebookContentProvider1854328631556764"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
**Layout Xml Code**
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
**Put own generate facebook Key in String.xml File**
<string name="facebook_app_id">1854328631556764</string>
<string name="fb_login_protocol_scheme">fb1854328631556764</string>
**MainActivity CODE**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
FacebookSdk.sdkInitialize(this.getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
fbloginButton = (LinearLayout) findViewById(R.id.facebook_button);
fbloginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
//showing dialog to select image
callFacebook();
Log.e("permission", "granted");
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA}, 1);
}
} else {
callFacebook();
}
}
});
}
**CAllOnFAcebook**
public void callFacebook() {
callbackManager = CallbackManager.Factory.create();
// LoginManager.getInstance().logInWithReadPermissions(MainActivity2.this, Arrays.asList("email"));
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e("keshav","onSuccess called ");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivityfacebook", response.toString());
try {
JSONObject data = response.getJSONObject();
facebookName = data.getString("name");
facebookEmail = data.getString("email");
facebookId = data.getString("id");
facebookLink = data.getString("link");
facebookLink = facebookLink.replace("https://www.facebook.com/app_scoped_user_id/", "");
facebookLink = facebookLink.replace("/", "");
Log.e("profilelink", "" + facebookLink);
facebookLink = "https://graph.facebook.com/" + facebookLink + "/picture";
Log.e("facebookName", "is -> " + facebookName);
Log.e("facebookEmail", "is -> " + facebookEmail);
Log.e("facebookId", "is -> " + facebookId);
Log.e("facebookLink", "is -> " + facebookLink);
Log.e("Keshav","Suceesfully Login "+facebookName);
} else {
CommonMethod.showAlert("Intenet Connectivity Failure", MainActivity.this);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,link");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.i(TAG, "cancelTriggered");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.i(TAG, "errorTriggered");
}
});
LoginManager.getInstance().logOut(); // TODO Add Me Using this previous facebook login id password logout
}
**Process Login With Facebook**
**Working on Play Store**
**Link https://play.google.com/store/apps/details?id=com.codecube.airbucks&hl=en**