【问题标题】:PHP oauth for linkedin用于linkedin的PHP oauth
【发布时间】:2012-03-30 15:23:33
【问题描述】:

我正在尝试使用来自linkedin 的快速入门指南进行 oauth 身份验证。我已经安装了 oauth 库。当我运行下面的代码时,// ask for the pin 之后我没有得到任何输出。当我执行echo STDIN 时,浏览器实际上会呈现STDIN 而不是STDIN 的值。为什么我没有看到供我输入密码的字段,如quick start guide 中的链接所述?

这是快速入门指南中的代码。我用自己的 API 密钥替换了。

<?php

// TODO change these to your API key and secret
define("API_CONSUMER_KEY", "xxxxxxxxxxxx");
define("API_CONSUMER_SECRET", "xxxxxxxxxxxx");

// create a new instance of the OAuth PECL extension class
$oauth = new OAuth(API_CONSUMER_KEY, API_CONSUMER_SECRET);

// get our request token
$api_url = "https://api.linkedin.com/uas/oauth/requestToken";
$rt_info = $oauth->getRequestToken($api_url);

// now set the token so we can get our access token
$oauth->setToken($rt_info["oauth_token"], $rt_info["oauth_token_secret"]);

// instruct on how to authorize the app
print("Please visit this URL:\n\n");
printf("https://www.linkedin.com/uas/oauth/authenticate?oauth_token=%s", $rt_info["oauth_token"]);
print("\n\nIn your browser and then input the numerical code you are provided here: ");

// ask for the pin  
$pin = trim(fgets(STDIN));

// get the access token now that we have the verifier pin
$at_info = $oauth->getAccessToken("https://api.linkedin.com/uas/oauth/accessToken", "", $pin);

// set the access token so we can make authenticated requests
$oauth->setToken($at_info["oauth_token"], $at_info["oauth_token_secret"]);

// do a simple query to make sure our token works
// we fetch our own profile on linkedin. This query will be explained more on later pages
$api_url = "http://api.linkedin.com/v1/people/~";
$oauth->fetch($api_url, null, OAUTH_HTTP_METHOD_GET);

// print_response is just a fancy wrapper around print and is defined later
// or you can look now and see it in the code download
print_response($oauth);

【问题讨论】:

    标签: php oauth linkedin


    【解决方案1】:

    我猜你正在运行一个用于终端的 PHP 脚本(执行 php myscript.php 在终端中)在服务器上下文中。并且服务器上下文不允许从 标准输入法。

    编写一个以$pin = "PIN I got from that URL" 开头的新PHP 文件,其余部分 从提供的脚本中,然后运行该脚本。并且,请注意print_response 函数,我不知道它们是什么意思:-)

    在您的示例中,LinkedIn 在该网页中显示令牌。这被称为 频段访问,对于不进行重定向的设备很有用,例如旧智能手机(AFAIK!)。 在正常工作流程中,将其配置为重定向到您的回调 URL mysite.com/oauth_client/authentication_success?token=TOKEN 并让该 URL 处理剩下的。

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2012-08-14
      • 1970-01-01
      • 2014-07-12
      • 2018-06-24
      相关资源
      最近更新 更多