2012-03-30 18 views
2

Ich probiere die Kurzanleitung von linkedin für oauth Authentifizierung aus. Ich habe die oauth-Bibliothek bereits installiert. Wenn ich den Code unten ausführe, erhalte ich keine Ausgabe nach // ask for the pin. Wenn ich echo STDIN mache, rendert der Browser wörtlich STDIN anstelle des Werts von STDIN. Warum sehe ich kein Feld für mich, um den Pin einzugeben, wie auf der verlinkten Seite in quick start guide beschrieben?PHP oauth für linkedin

Hier ist der Code aus der Kurzanleitung. Ich habe die API-Schlüssel durch meine eigenen ersetzt.

<?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); 

Antwort

5

Ich denke, Sie ein PHP-Skript für das Terminal sollte ausgeführt werden (dabei ein php myscript.php im Terminal) in einem Serverkontext. Und der Serverkontext erlaubt keine Lesevorgänge von STDIN.

Schreiben Sie eine neue PHP-Datei, die mit $pin = "PIN I got from that URL" beginnt, und den Rest aus dem bereitgestellten Skript, und führen Sie dann das Skript aus. Und beware die print_response Funktion, ich weiß nicht, was sie bedeuten :-)

In Ihrem Beispiel zeigt LinkedIn das Token auf dieser Webseite. Dies wird aus Bandzugriff aufgerufen, nützlich für Geräte, die keine Weiterleitungen wie alte Smartphones (AFAIK!) Tun. In normalen Workflows, konfigurieren Sie es auf Ihre Callback-URL mysite.com/oauth_client/authentication_success?token=TOKEN umleiten und lassen Sie diese URL den Rest erledigen.