Я пытаюсь получить имя и адрес электронной почты после входа в систему через LinkedIn, но пока мне не повезло. Я успешно делал запросы через Post Man, даже используя токен доступа для получения имени и адреса электронной почты, но попытка применить его здесь не работает. Давать мне ошибки о политике CORS. В моей функции getAccessToken открытие XMLHTTPRequest дает мне только readyState. Что я делаю неправильно? Я искал и пытался в течение нескольких дней. Я использую только Javascript и HTML. Спасибо.
Я видел этот ответ здесь , который был тем, что закрыл мой предыдущий пост об этом, но он говорит, что невозможно сделать запрос через интерфейс, который является чем-то, что Я не могу сделать. Хотелось бы узнать, есть ли способ сделать это так, как я делаю.
<body>
<!-- Displays a button to let the viewer authenticate -->
<script type="IN/Login"></script>
<form action="" method="POST">
<!-- Login through LinkedIn -->
<a href="https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=myClientID&redirect_uri=http%3A%2F%2Flocalhost%2FIPSForm%2FtestLinkedIn.html&state=fooobar&scope=r_liteprofile%20r_emailaddress%20w_member_social"> Click me</a>
</br>
<br>
<!-- Trying to get info from the access token given -->
<a href=""
onclick="getAccessToken()">Click for authorization page</a>
<!-- Placeholder for the greeting -->
<div id="testThis" class="testThis"></div>
<div id="profiles"></div>
<input type="text" id="inputForText">
</form>
</body>
<script>
window.addEventListener("load", getAccessTokenTest);
function getAccessToken() {
debugger;
let params = new URLSearchParams(location.search);
if(params.get("code") != null) {
console.log(`Params while getting accesing token function: ${params.get("code")}`);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", `https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&client_id=myClientID&client_secret=myClientSecret&redirect_uri=http://localhost/IPSForm/testLinkedIn.html&code=${params.get('code')}`, true);
xhr.addEventListener("readystatechange", function(){
if(this.readyState == 4) {
console.log(this.responseText);
}
});
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send();
authorized(params);
} else {
return;
}
}
function authorized(accessToken) {
console.log("Reached beginning of authorized function");
var urlAccess = `https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&client_id=myClientID&client_secret=myClientSecret&redirect_uri=http://localhost/IPSForm/testLinkedIn.html&code=${ accessToken }`;
var xhttp2 = new XMLHttpRequest();
xhttp2.withCredentials = true;
xhttp2.open("GET", "https://api.linkedin.com/v2/me/", true);
xhttp2.addEventListener("readystatechange", function() {
if(this.readyState == 4) {
console.log(`This is the response text - ${this.responseText}`);
}
});
alert(`url readyState - ${xhttp2.readyState} \n url stats - ${xhttp2.status}`);
xhttp2.setRequestHeader("Authorization", `Bearer ${accessToken}`);
xhttp2.send();
}
</script>
</html>
Вот изображение ошибок https://i.stack.imgur.com/CHv98.png