简体   繁体   English

如何将Facebook SDK集成到本机Android应用中

[英]How to integrate Facebook SDK into Native Android apps

Facebook has a very good documentation of integrating SDKs for Android, iOS & JavaScript etc. But still I have seen some confusions or misunderstandings regarding the exact ways to integrate Facebook SDK. Facebook拥有非常好的文档 ,它们集成了适用于Android, iOS & JavaScript等的SDK。但是,关于集成Facebook SDK的确切方法,我仍然看到一些困惑或误解。

So I am writing all to the point steps to integrate Facebook SDK in Android Native apps and make use of Graph APIs etc. 因此,我正在编写所有关键步骤,以将Facebook SDK集成到Android Native应用程序中,并利用Graph API等。

In documentation, they have explained it in the way of documentation. 在文档中,他们以文档的方式进行了解释。

Here I am trying to explain the steps the way we need them while integrating in our app. 在这里,我试图解释在集成到我们的应用程序时所需的步骤。

I will be giving answers to the most doubts we get, most occurring errors etc. 我将给出对我们所获得的大多数疑问,最常见的错误等的答案。

I hope it will help someone someday. 希望有一天能对某人有所帮助。

Facebook provides a wide range of options that can be done by integrating its SDK. Facebook提供了广泛的选项,可以通过集成其SDK来完成。 But we need to follow some proper steps for the same. 但是我们需要遵循一些适当的步骤。

  • Login 登录

The very first thing we need to do is Login , before our app starts making use of Facebook SDK we need to do authentication . 我们需要做的第一件事是Login ,在我们的应用开始使用Facebook SDK之前,我们需要进行authentication The user must login with his/her account before our app starts getting benefit from 用户必须先使用自己的帐户登录,然后我们的应用才能开始从中受益

This can be done in two ways, 这可以通过两种方式完成:

a) Via Native Android App a) 通过本机Android应用

If the user is having Facebook app installed in their phones then they will be asked to login via the account they have logged in with in their facebook app. 如果用户的手机中安装了Facebook应用程序,则将要求他们通过其在Facebook应用程序中登录时使用的帐户登录。

b) Via Web View b) 通过Web视图

If user is not having facebook app, then facebook login page is opened in a web browser and user can login to their account. 如果用户没有Facebook应用,则将在Web浏览器中打开Facebook登录页面,并且用户可以登录其帐户。

So by any of these two options, once the user logs in then your app gets authenticated to use their account in your app. 因此,通过这两个选项中的任何一个,一旦用户登录,您的应用便会通过身份验证以使用其帐户在您的应用中。

How to do this 这个怎么做

Once you include Facebook SDK & Library project in your own Android app then you can simply put their built in widget in any of Your xml file and you will see the Login button there. 将Facebook SDK和Library项目包含在自己的Android应用中后,您只需将其内置的小部件放在您的xml文件中的任何一个中,即可在其中看到“登录”按钮。 It's code is simple, 它的代码很简单,

<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"
        />

Just include the above mentioned code in your XML file (where ever you want to show Facebook Login button.) It will automatically create the Login button like the following, 只需将上述代码包含在XML文件中(无论您想显示Facebook登录按钮的位置。),它都会自动创建“登录”按钮,如下所示,

在此处输入图片说明

Confusions/Doubts/Questions we have in our minds up till this stage 到现在为止,我们脑海中一直存在的困惑/疑问/问题

Q. How can I create the button like this in my own app? 问:如何在自己的应用中创建这样的按钮? From where am I going to get logo or what color does it have etc. etc. 我将从哪里获得徽标或徽标具有什么颜色等。

Ans. 答。 We don't need to create it on our own, we just put that widget in our code and the button is created automatically. 我们不需要自己创建它,我们只需要将该小部件放在代码中即可自动创建按钮。

  • Authorization 授权

Okay, fine we have included the Facebook login button, now user can do login into our app, next what? 好的,好的,我们已经添加了Facebook登录按钮,现在用户可以登录我们的应用程序了,接下来呢?

Next Facebook provides many kind of options we can do, like Share something on their profile, Post a status or custom story, get user's liked pages, access their messages, posts, feeds, friend list blah blah blah. 下一步Facebook提供了我们可以选择的多种选项,例如在其个人资料上共享某些内容,发布状态或自定义故事,获取用户喜欢的页面,访问其消息,帖子,提要,好友列表等等。

But it is not like that user logged in with their account and we can do anything we want in our app with their facebook account. 但这不像该用户使用其帐户登录,我们可以使用其Facebook帐户在我们的应用程序中执行任何所需的操作。

The next term which comes in to action is Permissions 生效的下一个术语是“ Permissions

To perform a specific task we need related permission, if the user will give you permission to do so with their Facebook account, only then our app will be able to do. 要执行特定任务,我们需要相关的权限,如果用户将使用您的Facebook帐户授予您这样做的权限,则只有我们的应用程序才能这样做。

For example, to access user's likes we need user_likes permission, to post something on behalf of user on their profile we need publish_actions permission etc. Complete list of permissions can be found here 例如,要访问用户的喜欢,我们需要user_likes权限,需要在用户的个人资料上代表用户发布内容,我们需要publish_actions权限等。权限的完整列表可在此处找到

So after authentication, the next thing is authorization . 因此,在身份验证之后,接下来就是授权 What our app is authorized to do with user's account. 我们的应用程序有权使用用户的帐户进行操作。

How to get permissions 如何获得权限

As we have already created a Login button for facebook using their button widget, now we can provide the permissions as a list to that button, so it will let the user know about what permissions you want from them when they login with their account. 由于我们已经使用他们的按钮小部件为facebook创建了一个登录按钮,现在我们可以将权限作为该按钮的列表提供,因此,当用户使用其帐户登录时,它将使用户知道您希望他们获得哪些权限。

Clearing the doubt here 在这里消除疑问

YES, the user will be told what permissions your app wants from them while doing login. 是的,在登录时,系统会告诉用户您的应用需要他们的应用程序权限。 Simply you can say, the user will be told what your app can do with their facebook account like accessing their friend list, post something on their profile, reading contacts etc. However the user may or may not read it, but the login button let them know about everything. 简单地说,您会被告知用户您的应用程序可以使用其Facebook帐户执行哪些操作,例如访问其朋友列表,在其个人资料上发布某些内容,阅读联系人等。但是,该用户可能会也可能不会阅读,但是登录按钮允许他们知道一切。

How to add permissions to Facebook login button 如何向Facebook登录按钮添加权限

LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status", "publish_actions"));

So you can see we are providing a list of permissions we want. 因此,您可以看到我们正在提供所需的权限列表。 However it is even possible to ask for more permissions later on, so it is more advisable to firstly ask for basic permissions and ask for permissions like publish_actions, when user would want it. 但是,以后甚至可以请求更多的权限,因此建议先请求基本权限,然后在用户需要时请求诸如publish_actions之类的权限。

  • Session 会议

Session is a very common term mostly in case of web applications. 会话是一个非常普遍的术语,主要用于Web应用程序。 It serves the similar purpose here. 在这里它起到类似的作用。 Once the user logs in with their facebook account, a Session is created in your app with the facebook and it remains until the user logs out. 用户使用其Facebook帐户登录后,便会在您的应用程序中使用Facebook创建一个SessionSession将一直保留到用户注销为止。

Q. How to show Facebook log out button? 问:如何显示Facebook注销按钮?

Ans. 答。 You will be very happy to hear this, you don't need to do anything to show log out button, once the user logs in, the login button is automatically changed to logout button. 您会很高兴听到这个消息,您无需执行任何操作即可显示注销按钮,一旦用户登录,登录按钮就会自动更改为注销按钮。

Benefit of Session 会议的好处

Session object has the authentication token that enables us to perform any action with Facebook SDK. 会话对象具有authentication token ,该authentication token使我们能够使用Facebook SDK执行任何操作。

We can get the facebook session object at any time by calling the following method, 通过调用以下方法,我们可以随时获取facebook会话对象,

Session session = Session.getActiveSession();

  • UiLifecycleHelper class UiLifecycleHelper类

Although we have done all the steps, but we need to maintain the session properly then we are going to need this class. 尽管我们已经完成了所有步骤,但是我们需要正确维护会话,然后才需要此类。 We just need to put it in all our activity life cycle methods so that the facebook session is maintained accordingly. 我们只需要将其放入所有活动生命周期方法中,即可相应地维护facebook会话。

Define an instance variable in your activity, 在您的活动中定义一个实例变量,

private UiLifecycleHelper uiHelper;

Then add the following code, 然后添加以下代码,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    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);
}

Now the facebook session will be maintained automatically without your headache. 现在,facebook会话将自动进行维护,而您无需担心。

  • Calling the Graph API 调用图谱API

I am going to give you a very simple and basic example here, suppose we want to get the list of the pages the user has liked, then we can do it with the following code, 在这里,我将为您提供一个非常简单且基本的示例,假设我们要获取用户喜欢的页面列表,然后可以使用以下代码完成操作,

Session session = Session.getActiveSession();
new Request(
    session,
    "/me/likes",
    null,
    HttpMethod.GET,
    new Request.Callback() {
        public void onCompleted(Response response) {

                 //Do what you want to with the response

        }
    }
).executeAsync();

Q. How it is going to fetch data related to the current user? 问:如何获取与当前用户有关的数据? We have not mentioned any user id any where? 我们在任何地方都没有提到任何用户ID?

Ans. 答。 The answer to this question is that we are using me object here, which will automatically point to the current user who has logged in to using facebook account. 这个问题的答案是我们在这里使用me对象,该对象将自动指向登录到使用Facebook帐户的当前用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM