-1

I learn about linkedin api so for start i need to know how i Request an Authorization Code ?

i try so many question with this type here, but i still understand.

From the docs is said to :

"Simple call" :

https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=123456789&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=987654321&scope=r_basicprofile

But i need to know how perform that call with php.

I try :

$response = file_get_contents("https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=123456789&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=987654321&scope=r_basicprofile");
$response = json_decode($response);
var_dump($response);

From this answer but my return is : NULL.

Edit

My oAuth2.0 end point authorized is http://localhost, i dont know if that made problem or are possible.

I put curl tag too, becouse the teaching for this purpose is very welcome.

sorry my english

MagicHat
  • 379
  • 1
  • 6
  • 28
  • http://hayageek.com/php-curl-post-get/#curl-get – Taha Paksu Nov 09 '17 at 16:12
  • Tks for atention @TahaPaksu , but with this example retun `404`, maybe some mistake on url request, i check many times the url parameters, souds ok, but not work... – MagicHat Nov 09 '17 at 16:20
  • Negative are welcome, when follow with any tips to be question more better, without that what benefit can it bring? – MagicHat Nov 12 '17 at 01:11

2 Answers2

0

Well i dont know if that is correct way, but i got with the follow:

To make the request :

<?php
header('Location: https:/www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=xxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%2FapiLinkedin%2FDevMagicHat%2Fauth%2Flinkedin/callback&state=xxxxxxxxx');
?>

And here is my call back, after request Linkdin answer on call back uri.

So in the folder callback i create an index.php page to receive response:

localhost/apiLinkedin/DevMagicHat/auth/linkedin/callback/index.php

<?php
$code = $_GET['code'];
$state = $_GET['state'];
echo "Code =>".$code."<br/>"."State =>".$state;
?>

i realy dont know if this way is better of others, but work fo me, i hope work fo you too.

MagicHat
  • 379
  • 1
  • 6
  • 28
0

First you have to create an application in Linkedin platform and get your client_id

We admit that your redirect url is: http://localhost/index.php, the first thing you should do is to direct the user's browser to LinkedIn's:

index.php

$params = [
    'response_type' => 'code',
    'client_id' => $client_id,
    'redirect_uri'  => 'http://localhost/index.php', 
    'state' => 'textOfYourChoice'
];

$url = 'https://www.linkedin.com/oauth/v2/authorization?'.http_build_query($params);

header('Location: '.$url);

You will get now the linkedin authorization page, just click allow button and you'll direct back to your page index.php with the code passed in the url parameters.

Now in order to get the code value you should check $_GET variable:

index.php becomes:

if (isset($_GET['code']) {
   die($_GET['code']);
}

$params = [
    'response_type' => 'code',
    'client_id' => $client_id,
    'redirect_uri'  => 'http://localhost/index.php', 
    'state' => 'textOfYourChoice'
];

$url = 'https://www.linkedin.com/oauth/v2/authorization?'.http_build_query($params);

header('Location: '.$url);
YouneL
  • 8,152
  • 2
  • 28
  • 50
  • i try something like that before using curl, but return `string 0`...] – MagicHat Nov 09 '17 at 17:49
  • @MagicHat, Ok Let me check, I didn't try this before, maybe there is something I forget – YouneL Nov 09 '17 at 18:10
  • Oh man, no problem, i realy desire, perform that with curl to learn... tks for atention...i wait ;) – MagicHat Nov 09 '17 at 18:12
  • There is no need to use curl here, check my answer again – YouneL Nov 09 '17 at 19:02
  • Man sorry my honesty, but your answer is the same like my, i say curl, becouse next step for my purpose is `POST` and cant `POST` with `header`function... I apreciate you energy to help, but answer like that is not welcome on SO... realy sry and tks... – MagicHat Nov 09 '17 at 19:15
  • Oh sorry I didn't see it – YouneL Nov 09 '17 at 19:18