Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2. define('API_KEY', 'kjsdadklasdij98' );
  3. define('API_SECRET', 'andlksalkdmllasd' );
  4. define('REDIRECT_URI', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']);
  5. define('SCOPE', 'r_fullprofile r_emailaddress' );
  6. session_name('linkedin');
  7. session_start();
  8.  
  9. if (isset($_GET['error'])) {
  10. // LinkedIn returned an error
  11. print $_GET['error'] . ': ' . $_GET['error_description'];
  12. exit;
  13. } elseif (isset($_GET['code'])) {
  14. // User authorized your application
  15. if ($_SESSION['state'] == $_GET['state']) {
  16. // Get token so you can make API calls
  17. getAccessToken();
  18. } else {
  19. // CSRF attack? Or did you mix up your states?
  20. exit;
  21. }
  22. } else {
  23. if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
  24. // Token has expired, clear the state
  25. $_SESSION = array();
  26. }
  27. if (empty($_SESSION['access_token'])) {
  28. // Start authorization process
  29. getAuthorizationCode();
  30. }
  31. }
  32.  
  33. // Congratulations! You have a valid token. Now fetch your profile
  34. $user = fetch('GET', '/v1/people/~:(skills:(id,skill:(name)))');
  35. print $user->skills;
  36. exit;
  37. function getAuthorizationCode() {
  38. $params = array(
  39. 'response_type' => 'code',
  40. 'client_id' => API_KEY,
  41. 'scope' => SCOPE,
  42. 'state' => uniqid('', true), // unique long string
  43. 'redirect_uri' => REDIRECT_URI,
  44. );
  45.  
  46. // Authentication request
  47. $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
  48.  
  49. // Needed to identify request when it returns to us
  50. $_SESSION['state'] = $params['state'];
  51.  
  52. // Redirect user to authenticate
  53. header("Location: $url");
  54. exit;
  55. }
  56.  
  57. function getAccessToken() {
  58. $params = array(
  59. 'grant_type' => 'authorization_code',
  60. 'client_id' => API_KEY,
  61. 'client_secret' => API_SECRET,
  62. 'code' => $_GET['code'],
  63. 'redirect_uri' => REDIRECT_URI,
  64. );
  65.  
  66. // Access Token request
  67. $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
  68.  
  69. // Tell streams to make a POST request
  70. $context = stream_context_create(
  71. array('http' =>
  72. array('method' => 'POST',
  73. )
  74. )
  75. );
  76.  
  77. // Retrieve access token information
  78. $response = file_get_contents($url, false, $context);
  79.  
  80. // Native PHP object, please
  81. $token = json_decode($response);
  82.  
  83. // Store access token and expiration time
  84. $_SESSION['access_token'] = $token->access_token; // guard this!
  85. $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds)
  86. $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time
  87.  
  88. return true;
  89. }
  90.  
  91. function fetch($method, $resource, $body = '') {
  92. //print $_SESSION['access_token'];
  93.  
  94. $opts = array(
  95. 'http'=>array(
  96. 'method' => $method,
  97. 'header' => "Authorization: Bearer " . $_SESSION['access_token'] . "rn" . "x-li-format: jsonrn"
  98. )
  99. );
  100.  
  101. // Need to use HTTPS
  102. $url = 'https://api.linkedin.com' . $resource;
  103.  
  104. // Append query parameters (if there are any)
  105. if (count($params)) { $url .= '?' . http_build_query($params); }
  106.  
  107. // Tell streams to make a (GET, POST, PUT, or DELETE) request
  108. // And use OAuth 2 access token as Authorization
  109. $context = stream_context_create($opts);
  110.  
  111. // Hocus Pocus
  112. $response = file_get_contents($url, false, $context);
  113.  
  114. // Native PHP object, please
  115. return json_decode($response);
  116. }
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement