使用 LinkedIn 注册
9 分
使用linkedin登录教程 介绍 使用linkedin登录允许用户使用他们的linkedin账户登录应用程序。 先决条件 要完成本教程,您需要: 在back4app创建的应用程序 查看 创建新应用教程 https //www back4app com/docs/get started/new parse app 以了解如何在back4app上创建应用程序。 为您的back4app应用设置子域名 查看 激活您的网络托管和实时查询 https //www back4app com/docs/platform/activating web hosting 以了解如何在back4app中创建子域名。 一个 linkedin开发者账户 https //www linkedin com/developers 。 1 创建一个新的back4app应用 首先,您需要确保在 back4app 上创建了一个现有的应用程序。如果您是新用户,可以查看 本教程 https //www back4app com/docs/get started/new parse app 以了解如何创建一个应用程序。 2 创建一个新的 linkedin 应用 登录您的 linkedin 开发者账户 https //www linkedin com/developers ,点击 \<font color="#2166ae">创建应用\</font> 并选择 \<font color="#2166ae">oauth 客户端 id\</font> 选择一个应用名称并填写所需字段,例如 \<font color="#2166ae">商业电子邮件\</font> 和 \<font color="#2166ae">应用程序徽标\</font> 同意条款并点击 \<font color="#2166ae">创建应用\</font> 在您新创建的应用程序中,点击 \<font color="#2166ae">验证\</font> 以验证应用程序的所有权。您必须是linkedin页面的所有者或管理员才能进行验证。 在验证页面中,点击 \<font color="#2166ae">生成url\</font> 使用公司页面的管理员或所有者帐户访问生成的 \<font color="#2166ae">验证url\</font> 。 点击 \<font color="#2166ae">批准验证\</font> 确保您的应用程序已验证 在您的应用中,转到 \<font color="#2166ae">认证\</font> 标签,填写 \<font color="#2166ae">重定向 url\</font> 字段,然后点击 \<font color="#2166ae">更新\</font> 3 获取您的代码 访问以下 url,替换 \<font color="#2166ae">client id\</font> , \<font color="#2166ae">redirect url\</font> , 和 \<font color="#2166ae">a random string\</font> 为您创建的值。 随机字符串是为了避免 csrf 攻击。 https //www linkedin com/oauth/v2/authorization?response type=code\&client id=client id\&redirect uri=redirect url\&state=a random string\&scope=r emailaddress 使用您的linkedin账户登录,重定向的网站将在url中包含您的代码: 仅复制url中的代码部分,并运行以下curl命令,替换值为 \<font color="#2166ae">your code\</font> , \<font color="#2166ae">your client id\</font> , \<font color="#2166ae">your client secret\</font> , 和 \<font color="#2166ae">your redirect uri\</font> 作为您应用程序的值 1 curl x post \\ 2 https //www linkedin com/oauth/v2/accesstoken \\ 3 h 'cache control no cache' \\ 4 h 'content type application/x www form urlencoded' \\ 5 d 'client id=your client id\&client secret=your client secret\&redirect uri=your redirect uri\&code=your code\&grant type=authorization code' 运行它,你应该能获取到你的访问令牌: 记住 该代码只能使用一次。如果你遇到错误或没有使用你的令牌,你必须重新生成你的代码才能再次运行它。 4 开始开发 现在linkedin的登录已配置好,你可以开始开发过程。 authdata的格式为: 1 { 2 "linkedin" { 3 "id" "user's linkedin id (string)", 4 "access token" "an authorized linkedin access token for the user", 5 "is mobile sdk" true|false // set to true if you acquired the token through linkedin mobile sdk 6 } 7 } 这是ios sdk的方法: 1 pfuser loginwithauthtype(inbackground "linkedin", authdata \["access token"\ tokenstring, "id" user, "is mobile sdk" true]) continuewith { task > any? in 2 3 } 这里是android sdk的方法: 1 map\<string, string, bool> authdata = new hashmap\<string, string, bool>(); 2 authdata put("access token", tokenstring); 3 authdata put("id", user); 4 authdata put("is mobile sdk", true); 5 task\<parseuser> t = parseuser loginwithinbackground("google", authdata); 6 t continuewith(new continuation\<parseuser, void>() { 7 public void then(task task) throws exception { 8 if (task iscancelled()) { 9 log w(tag, "task cancelled"); 10 } else if (task isfaulted()) { 11 log w(tag, "save fail" + task geterror()); 12 utilities showtoast(getresources() getstring(r string errorlogin) + task geterror(), mainactivity this); 13 } else { 14 // the object was saved successfully 15 parseuser user = (parseuser)task getresult(); 16 log w(tag, "success " + user getobjectid() + " " + user getusername() + " " + user getemail() + " " + user getsessiontoken()); 17 } 18 } 19 }