2012-03-30 86 views
2

我正在嘗試從oauth身份驗證的linkedin快速入門指南。我已經安裝了oauth庫。當我運行下面的代碼時,我在// ask for the pin之後沒有得到任何輸出。當我做echo STDIN時,瀏覽器實際上呈現STDIN而不是STDIN的值。爲什麼我沒有看到一個字段讓我輸入鏈接中描述的引腳quick start guide對於linkedin的PHP oauth

下面是快速入門指南中的代碼。我用自己的API替換了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); 

回答

5

我猜你正在運行意味着在服務器環境中的終端(做在終端中的php myscript.php )的PHP腳本。並且服務器上下文不允許從 STDIN讀取。

從提供的腳本中編寫一個以$pin = "PIN I got from that URL"和其餘 開頭的新PHP文件,然後運行該腳本。並且,請注意print_response 函數,我不知道它們是什麼意思:-)

在您的示例中,LinkedIn在該網頁中顯示令牌。這被稱爲 頻段訪問,對於不重定向的設備非常有用,如舊智能手機(AFAIK!)。 在正常的工作流程中,將其配置爲重定向到您的回調URL mysite.com/oauth_client/authentication_success?token=TOKEN,並讓該URL處理其餘的網址 。