Automating Linkedin oAuth usin Curl and PHP

若如初见. 提交于 2019-12-11 13:00:00

问题


I am trying to automate the process of authenticating LinkedIn login in order to perform a public search for people on LinkedIn.

First i will try to explain what i was doing.

I am using four files:

  • oAuth.php (required)
  • linkedin.php (php LinkedIn library)
  • auth.php (which gets oAuth token from the LinkedIn lib file)
  • callback url demo.php?params (which, after successful authenticaton, prints the current user's profile and the search results using params)

The authentication url is https://api.linkedin.com/uas/oauth/authorize?oauth_token=$oauthtoken.

I did two things, neither seems to work; they are:

  1. I am using curl to automate the process of going to the authentication url, posting fields (username, password, oauth token, csrfToken, duration, sourceAlias, etc., which I came to know from Firebug).

    The only two things which change here are oauth token and csrfToken (by parsing the content in the authentication url). I was able to get both, each time the page loads, and finally trying to print the GET response from curl_exec.

  2. Trying to post just the email and password, and trying to print a GET response.

For reference here is my auth.php:

function extract_unit($string, $start, $end)
{
    $pos = stripos($string, $start);
    $str = substr($string, $pos);
    $str_two = substr($str, strlen($start));
    $second_pos = stripos($str_two, $end);
    $str_three = substr($str_two, 0, $second_pos);
    $unit = trim($str_three); // remove whitespaces
    return $unit;
}

session_start();

$config['base_url']             =   'http://linkedin.tweetrank.tk/auth.php';
$config['callback_url']         =   'http://linkedin.tweetrank.tk/demo.php';
$config['linkedin_access']      =   'my key';
$config['linkedin_secret']      =   'my secret';

include_once "linkedin.php";

# First step is to initialize with the consumer key and secret. We'll use
# an out-of-band oauth_callback
$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url']);
//$linkedin->debug = true;

# Now we retrieve a request token. It will be set as $linkedin->request_token
$linkedin->getRequestToken();
$_SESSION['requestToken'] = serialize($linkedin->request_token);

# With a request token in hand, we can generate an authorization URL, which
# we'll direct the user to
//echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "\n\n";

echo $url = $linkedin->generateAuthorizeUrl();
$token = $linkedin->generateAuthorizeToken();
//echo '<br><br>';
$data = file_get_contents($url);
$csrfToken = extract_unit($data,'name="csrfToken" value="','"');

//echo $csrfToken;
//echo $token;
//echo 'https://www.linkedin.com/uas/oauth/authenticate?oauth_token='.$token.'&amp;trk=uas-continue';
// INIT CURL

$postParams = 'email=myemail&password=mypassword&duration=720&authorize=Ok%2C+I%27ll+Allow+It&extra=&access=-3&agree=true&oauth_token='.$token.'&appId=&csrfToken='.$csrfToken.'&sourceAlias=0_8L1usXMS_e_-SfuxXa1idxJ207ESR8hAXKfus4aDeAk';

Now I used the authentication URL and the postParams with curl.


回答1:


In order to get login process through, one has to use linkedIn web page authorization step; No way we can have third party app accepting credentials and making linkedIn authorization with the link https://api.linkedin.com/uas/oauth/authorize?oauth_token=$oauthtoken




回答2:


I think I understand what you are asking and the answer is no you cannot do that with LinkedIn. I have been facing similar problem recently and wasn't able to solve it. I guess the LinkedIn guys have a good point about data protecting and stuff. Check this out: http://developer.linkedin.com/message/6460 http://getsatisfaction.com/linkedin/topics/cant_use_linkedin_api_to_view_public_profiles_without_oauth_login



来源:https://stackoverflow.com/questions/4190164/automating-linkedin-oauth-usin-curl-and-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!