dsdukbc60905239 2012-03-30 01:14
浏览 205
已采纳

PHP oauth for linkedin

I'm trying out the quick start guide from linkedin for oauth authentication. I've already installed the oauth library. When i run the code below, I don't get any output after // ask for the pin. When I do echo STDIN, the browser literally renders STDIN instead of the value of STDIN. Why don't I see a field for me to input the pin as described on the linked in quick start guide?

Here's the code from the quick start guide. I replaced the API keys with my own.

<?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:

");
printf("https://www.linkedin.com/uas/oauth/authenticate?oauth_token=%s", $rt_info["oauth_token"]);
print("

In 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);
  • 写回答

1条回答 默认 最新

  • doudouxuqh198138 2012-03-30 07:23
    关注

    I guess you are running a PHP script meant for the terminal (doing a php myscript.php in the terminal) in a server context. And the server context does not allow reads from STDIN.

    Write a new PHP file that starts with $pin = "PIN I got from that URL" and the rest from the provided script and then run that script. And, beware the print_response function, I dont know what they mean :-)

    In your example, LinkedIn displays the token in that webpage. This is called out of band access, useful for devices that dont do redirects, like old smartphones (AFAIK!). In normal workflows, configure it to redirect to your callback URL mysite.com/oauth_client/authentication_success?token=TOKEN and let that URL handle the rest.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?