What is third-party login?

To put it simply, users can log in our own application through the account of the existing website or application. Such as common QQ, wechat, Weibo; The effect is that if you do not need to register an account again in our application, you can directly log in to our application using QQ, wechat, Weibo and other platform accounts. As shown in figure:

What problems do third-party logins solve?

Lower the threshold for users to use the product; Because, register and log in, it is really a more troublesome thing.

Second, the principle of

Basic Principles:

When the user clicks the third-party login, the third-party login SDK is redirected. The user enters the user name or password of the third-party login platform. Some third-party login platforms can directly call the account that has been logged in, such as QQ. Have completed the third-party platform login; After login, the third-party platform or SDK will call back our application. In the callback information, we can get the user’s OpenId, nickname, profile picture and other information on the third-party platform.

The detailed process is as follows:

(A) After the user opens the client, the client requires the user to grant authorization. (B) The User agrees to authorize the Client. (C) The client applies for a token from the authentication server using the authorization obtained in the previous step. (D) After authenticating the client, the authentication server confirms that it is correct and agrees to issue the token. (E) The client uses the token to apply to the resource server for obtaining resources. (F) The resource server confirms the token and agrees to open the resource to the client

For more detailed principle learning, please refer to the following chain. This article focuses on the implementation process of OAuth 2 for OAuth 2 Simplified • Aaron Parecki

Three, the implementation process

1. Create applications on third-party service platforms

This step is almost all of the steps required to integrate third-party services.

User name, application type, application platform (iOS, Android, Web), BundleId and other information may need to be provided according to different platforms and services. The following uses the QQ login service as an example

1) Enter QQ Open platform and access application application

2) Start creation, enter the creation page, and click “Create Application”.

Note: the first time to use the general identity authentication, self-authentication on the line

3) Then fill in the name and signature information of the installation package, submit it and wait for the review. At this time, the login interface can be called, but there are some restrictions. After passing the review, it can be called completely, generally within 3 days

4) As shown in figure after approval:

5) Click to view and get the APP ID and Key:

These two pieces of information are used when integrating the SDK

2. Obtain application information

Can be obtained from 5) above

3. The client integrates SDK

ShareSDK is used here because:

  1. It integrates many third-party platforms, including QQ, wechat, Weibo, Alipay, etc.
  2. Can achieve sharing, third-party login and other functions.
  3. Provide unified login and sharing interface externally; It hides the API differences between different SDKS, so it makes it easier to integrate QQ, wechat, Weibo, Alipay and other platforms.

Although I created an application in QQ before, because I used ShareSDK, which is also a third-party service, I also need to create an application. Finally, I need to obtain information like AppKey. The process is similar to QQ, which will not be described here

SharedSDK executes sequence diagrams (same underlying principle as OAuth2 above) :

Complete integration steps:MobTech Integration Documentation -MobTech

Initialize the SDK

4. The business invokes the SDK interface to achieve three-party login

The following code to click the login button, to achieve three-party QQ login as an example:

public void onQQLoginClick(a) {
    // Initialize the specific platform
    Platform platform = ShareSDK.getPlatform(QQ.NAME);

    // Set false to SSO authorization
    platform.SSOSetting(false);

    // Callback information
    // Basic authorization returns can be obtained here
    platform.setPlatformActionListener(new PlatformActionListener() {
        /** * Login succeeded *@param platform
         * @param i
         * @param hashMap
         */
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            // The login succeeded

            // Get the name, avatar, OpenId
            // The method callback is not on the main thread

            // Get information from the database
            // Can also be obtained with the user argument
            PlatformDb db = platform.getDb();

            data = new User();

            PlatformDb db = platform.getDb();
            String nickname = db.getUserName();
            String avatar = db.getUserIcon();
            String openId = db.getUserId();

            LogUtil.d(TAG, "other login success:" + nickname + "," + avatar + "," + openId + "," + HandlerUtil.isMainThread());
        }

        /** * Login failed *@param platform
         * @param i
         * @param throwable
         */
        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            LogUtil.d(TAG, "other login error:" + throwable.getLocalizedMessage() + "," + HandlerUtil.isMainThread());
        }

        /** * cancel login *@param platform
         * @param i
         */
        @Override
        public void onCancel(Platform platform, int i) {
            LogUtil.d(TAG, "other login cancel:" + i + ","+ HandlerUtil.isMainThread()); }});//authorize and showUser respectively
    // Authorize and obtain user information
    platform.showUser(null);
}
Copy the code

In this way, we have achieved the purpose of tripartite login, we can get the tripartite application information, show it to our corresponding client

Other interface description:

Note:

This is only the process of three-party login. In the online project, the process will be more complicated. For example, the login interface in the background is often needed to judge whether the login is successful. If the message "User does not exist" is displayed, the user is not registered. The client switches to the page for adding user information. After the user information is added, the user registration logic is invokedCopy the code

You can refine it according to the business scenario