如何实施“使用LinkedIn登录"使用"OAuth 2.0"在Android中 [英] How to implement "Login with LinkedIn" with "OAuth 2.0" in Android

查看:172
本文介绍了如何实施“使用LinkedIn登录"使用"OAuth 2.0"在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OAuth1.0中使用Linkedin登录"可以正常工作,但在几天前Linkedin对其政策进行了一些更改,请参阅以下链接以获取更多详细信息, https://engineering.linkedin.com/blog/2018/12/developer -程序更新

我也尝试了一些GitHub示例和其他参考,但不幸的是对我不起作用,我也尝试了下面的链接,但没有满足确切的要求.

针对LinkedIn的Oauth 2.0授权在Android中

您还可以在下面的链接中引用它,可能会产生一些结果,我也知道Linkedin SDK在这里声明不起作用, https://developer.linkedin.com/docs/android-sdk 我们必须调用手动URL并打开Webview.

https://docs.microsoft.com/zh-CN/linkedin/consumer/integrations/self-serv/sign-in-with-linkedin?context=linkedin/consumer/context

我还检查了3-4个先前具有LinkedIn支持身份验证的应用程序,但现在他们将其删除了,但是我检查了一个名为"Nuzzel"的应用程序,因为我发现Linked在身份验证中工作正常,因此这意味着可以使用某种方法正确地做. Nuzzel应用程序链接:- https://play. google.com/store/apps/details?id=com.nuzzel.android&hl=zh_CN

谢谢

解决方案

经过一番努力,我使它起作用了,但是每次启动一个新项目时实施它都会浪费很多时间.

因此,我为此创建了一个轻量级的.

只需将依赖项添加到您的应用级build.gradle文件

dependencies {
    implementation 'com.shantanudeshmukh:linkedinsdk:1.0.0'
}

发起登录请求.

LinkedInBuilder.getInstance(MainActivity.this)
        .setClientID("<YOUR_CLIENT_ID_HERE>")
        .setClientSecret("<YOUR_CLIENT_SECRET_HERE>")
        .setRedirectURI("<YOUR_REDIRECT_URL_HERE>")
        .authenticate(LINKEDIN_REQUEST_CODE);

处理响应:

     @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == LINKEDIN_REQUEST_CODE && data != null) {
            if (resultCode == RESULT_OK) {
                //Successfully signed in
                LinkedInUser user = data.getParcelableExtra("social_login");

                //acessing user info
                Log.i("LinkedInLogin", user.getFirstName());

            } else {

                if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_USER_DENIED) {
                    //Handle : user denied access to account

                } else if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_FAILED) {

                    //Handle : Error in API : see logcat output for details
                    Log.e("LINKEDIN ERROR", data.getStringExtra("err_message"));
                }
            }
        }

    }

 

我正在多个生产应用程序中使用此库,因此我将尝试使其尽可能保持更新.您可以在此处找到其他详细信息.

In OAuth1.0 "Login with Linkedin" Working fine but before few days Linkedin make some changes in their policy, please refer below link for more detail, https://engineering.linkedin.com/blog/2018/12/developer-program-updates

I also tried some GitHub example and other reference but unfortunately not work for me, I also tried below link but it did not fulfill the exact requirement.

Oauth 2.0 authorization for LinkedIn in Android

You can also refer below link, may it give some result, I also know Linkedin SDK not work here as they declared here, https://developer.linkedin.com/docs/android-sdk We have to call manual URL and open into Webview.

https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin?context=linkedin/consumer/context

I also check 3-4 apps which earlier had LinkedIn support for authentication but now they removed it but I check one app namely "Nuzzel" in that I found Linked in authentication and it's working fine so it means there is some way to do it properly. Nuzzel App Link:- https://play.google.com/store/apps/details?id=com.nuzzel.android&hl=en_GB

Thanks

解决方案

After a little effort I got it working, but implementing it every time you start a new project will waste a lot of time.

So I created a light weight library for the same.

Just add the dependency to your app level build.gradle file

dependencies {
    implementation 'com.shantanudeshmukh:linkedinsdk:1.0.0'
}

Initiate Login Request.

LinkedInBuilder.getInstance(MainActivity.this)
        .setClientID("<YOUR_CLIENT_ID_HERE>")
        .setClientSecret("<YOUR_CLIENT_SECRET_HERE>")
        .setRedirectURI("<YOUR_REDIRECT_URL_HERE>")
        .authenticate(LINKEDIN_REQUEST_CODE);

Handle the response:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == LINKEDIN_REQUEST_CODE && data != null) {
            if (resultCode == RESULT_OK) {
                //Successfully signed in
                LinkedInUser user = data.getParcelableExtra("social_login");

                //acessing user info
                Log.i("LinkedInLogin", user.getFirstName());

            } else {

                if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_USER_DENIED) {
                    //Handle : user denied access to account

                } else if (data.getIntExtra("err_code", 0) == LinkedInBuilder.ERROR_FAILED) {

                    //Handle : Error in API : see logcat output for details
                    Log.e("LINKEDIN ERROR", data.getStringExtra("err_message"));
                }
            }
        }

    }

I'm using this library in multiple production apps, so I'll try to keep it as updated as possible. You can find other details here.

这篇关于如何实施“使用LinkedIn登录"使用"OAuth 2.0"在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆