使用 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中创建子域名。 一个 https //www linkedin com/developers 。 1 创建一个新的back4app应用 首先,您需要确保在 back4app 上创建了一个现有的应用程序。如果您是新用户,可以查看 https //www back4app com/docs/get started/new parse app 以了解如何创建一个应用程序。 2 创建一个新的 linkedin 应用 登录您的 https //www linkedin com/developers ,点击 创建应用 创建应用 并选择 oauth 客户端 id oauth 客户端 id 选择一个应用名称并填写所需字段,例如 商业电子邮件 商业电子邮件 和 应用程序徽标 应用程序徽标 同意条款并点击 创建应用 创建应用 在您新创建的应用程序中,点击 验证 验证 以验证应用程序的所有权。您必须是linkedin页面的所有者或管理员才能进行验证。 在验证页面中,点击 生成url 生成url 使用公司页面的管理员或所有者帐户访问生成的 验证url 验证url 。 点击 批准验证 批准验证 确保您的应用程序已验证 在您的应用中,转到 认证 认证 标签,填写 重定向 url 重定向 url 字段,然后点击 更新 更新 3 获取您的代码 访问以下 url,替换 client id client id , redirect url redirect url , 和 a random string a random string 为您创建的值。 随机字符串是为了避免 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命令,替换值为 your code your code , your client id your client id , your client secret your client secret , 和 your redirect uri your redirect uri 作为您应用程序的值 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 }