无法在 Oauth 中获取链接的访问令牌 [英] Unable to get access Token linkedin Oauth

查看:31
本文介绍了无法在 Oauth 中获取链接的访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有些人会发表评论,比如这篇文章重复了很多问题,但我已经尝试了很多方法来在linkedin Oauth中实现访问令牌.解释我的尝试.

I know some will put comment like this post is duplicate of so many questions, but I've tried many ways to achieve Access Token in linkedin Oauth. Explaining what i tried.

1) 我正在关注它的官方文档 Linkedin Oauth2

1) I'm following it's official doc's Linkedin Oauth2

2) 我成功地从第 2 步获取授权代码并将该代码传递到第 3 步以交换授权代码以获取访问令牌.但我收到以下错误{"error_description":"缺少必需的参数,包含无效的参数值,参数不止一次.:无法检索访问令牌:appId 或重定向 uri 与授权代码不匹配或授权代码已过期","错误":无效请求"}

2) I'm successfully getting Authorization code from step 2 and passing that code to step 3 for exchanging Auth code for getting Access Token. But i'm getting following error {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired","error":"invalid_request"}

3) 根据一些链接,我需要在标题中设置 content-type.缺少设置内容类型的链接

3) According to some links i need to set content-type in the header.Link which tells to set content-type is missing

4) 然后我尝试调用 https://www.linkedin.com/uas/oauth2/accessToken 这个服务而不是 POStGET.并将数据作为 queryParams 传递.

4)Then i tried calling https://www.linkedin.com/uas/oauth2/accessToken this service instead of POSt to GET. And passing data as queryParams.

5) 一些 link 说 oauth 代码会在 20 秒后过期, 所以我已经检查过了,我正在 1 秒内调用访问令牌.

5) Some link says oauth code expires in 20 sec, So i've checked, i'm making call for access token in less that 1 sec.

6) 如果我像下面这样在 Body 参数中传递数据并使用 url 作为 https://www.linkedin.com/uas/oauth2/accessToken

6) And if i pass data in Body params like as below and used url as https://www.linkedin.com/uas/oauth2/accessToken

var postData = {
                grant_type: "authorization_code",
                code: authCode,
                redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
                client_id: 'my_client_id',
                client_secret: 'secret_key'
            };

7) 用 Get 调用我的 url 我试过https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code='+authCode+'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&;client_secret=secret_key

7) With Get call my url i tried https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code='+authCode+'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key

即使状态代码为 200,我仍然收到错误,我收到该错误(使用 GET api)如果 POSt 通过在 body 中传递 postData 我得到了错误的请求 400 状态代码

Still i'm getting Error even though status code is 200, i'm getting that error(with GET api) and If POSt by passing postData in body i'm getting bad request 400 status code

不明白为什么我没有获得访问代码.我已经阅读了很多解决方案.按要求共享代码.

Not understanding why m I not getting access code. I've read many solutions. Sharing code as requested.

sap.ui.define([
	"sap/ui/core/mvc/Controller",
	"sap/m/MessageToast"
], function (Controller, MessageToast) {
	"use strict";

	return Controller.extend("OauthTest.OauthTest.controller.View1", {
		onPress: function (evt) {
			var sPath =
				'https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=my_client_id&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&state=DCEeFWf45A53sdfKef424&scope=r_basicprofile';
			window.location.href = sPath;
            var oRouter = new sap.ui.core.UIComponent.getRouterFor(this);
			oRouter.navTo("View2", {
				"username": "Test"
			});
			MessageToast.show(evt.getSource().getId() + " Pressed");
		},
		
		//after user allows access, user will be redirected to this app with code and state in URL
		//i'm fetching code from URL in below method(call is happening in max.569ms)
		
		onAfterRendering: function () {
			var currentUrl = window.location.href;
			var url = new URL(currentUrl);
			var authCode = url.searchParams.get("code");
			if (authCode !== undefined && authCode !== null) {
				var postData = {
					grant_type: "authorization_code",
					code: authCode,
					redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
					client_id: 'my_client_id',
					client_secret: 'secret_key'
				};
				
			/*	var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=' + authCode +'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key';*/

				var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken';

				$.ajax({
					url: accessTokenUrl,
					type: "POST",
					beforeSend: function (xhr) {
						xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					},
					data: postData,
					success: function (data, textStatus, jqXHR) {
						console.log(data);
						alert('success');
					},
					error: function (jqXHR, textStatus, errorThrown) {
						console.log(errorThrown);
						alert('error');
					}
				});
			}
		}

	});
});

将得到帮助..!!!

推荐答案

经过如此多的搜索,最后我很高兴发布我的答案.我所做的每一步都是正确的,但我在这里遗漏了一件事,例如 Linkedin API 不支持 CORS.

Finally I am happy to post my answer after so much search. Every step I did is correct only, but one thing I was missing here like, Linkedin API doesn't supports CORS.

我尝试实现 Javascript SDK,它的作用就像魅力一样.但 API 不是.然后我发现非常有用的 Link 说我需要通过允许从后端实现 Rest APICORS,不是来自前端.

I tried implementing Javascript SDK, that works like charm. But API wasn't. Then I found very helpful Link which says I need to implement Rest API from backend by allowing CORS, not from front end.

请务必遵循我在帖子中提到的所有要点.对于允许 CORS,请点击此链接.您将获得数据,但只能根据 LinkedIn 条款获得用户的基本资料可以访问数据

Make sure to follow all the points which I mentioned above in my post. And for Allow CORS follow this link. You will get data but only basic profile of user according to LinkedIn Terms data can be accessible

希望这篇文章可以帮助别人有时间搜索更多

Hope this post may help someones time to search more

这篇关于无法在 Oauth 中获取链接的访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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