使用 Curl 和 PHP 自动化 Linkedin oAuth

发布于 2024-10-02 14:32:04 字数 2759 浏览 4 评论 0原文

我正在尝试自动化 LinkedIn 登录身份验证过程,以便在 LinkedIn 上对人员进行公开搜索。

首先,我将尝试解释我在做什么。

我使用四个文件:

  • oAuth.php(必需)
  • linkedin.php(php LinkedIn 库)
  • auth.php(从LinkedIn lib 文件)
  • 回调 url demo.php?params (成功验证后,使用 params 打印当前用户的个人资料和搜索结果)

验证 url 为 https://api .linkedin.com/uas/oauth/authorize?oauth_token=$oauthtoken

我做了两件事,似乎都不起作用;它们是:

  1. 我正在使用curl来自动执行访问身份验证url、发布字段(用户名、密码、oauth令牌、csrfToken、持续时间、sourceAlias等,我从Firebug中了解到的)的过程。< /p>

    这里唯一改变的两件事是 oauth token 和 csrfToken(通过解析身份验证 url 中的内容)。每次页面加载时,我都能获得这两个结果,并最终尝试打印来自curl_exec的GET响应。

  2. 尝试仅发布电子邮件和密码,并尝试打印 GET 响应。

作为参考,这里是我的 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';

现在我使用了身份验证 URL 和带有 curl 的 postParams

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.'&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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-10-09 14:32:04

为了完成登录过程,必须使用linkedIn网页授权步骤;我们不可能让第三方应用程序接受凭据并通过链接进行 linkedIn 授权 https://api.linkedin.com/uas/oauth/authorize?oauth_token=$oauthtoken

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

风情万种。 2024-10-09 14:32:04

我想我明白你在问什么,答案是否定的,你不能用 LinkedIn 做到这一点。我最近也遇到了类似的问题,但无法解决。我想 LinkedIn 的人在数据保护之类的方面有很好的观点。
看看这个:
http://developer.linkedin.com/message/6460
http://getsatisfaction.com/linkedin/topics/cant_use_linkedin_api_to_view_public_profiles_without_oauth_login

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文