2012-04-17 8 views
3

以下は、oauthとoauthの両方の秘密情報を取得するために使用したものです。ユーザーアクセストークンとアクセストークンの秘密はどのように取得できますか?Twitterユーザートークンとアクセストークンの秘密を取得する

私は上記のトークンに事前に

require("twitteroauth/twitteroauth.php"); 
// The TwitterOAuth instance 
$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET'); 
// Requesting authentication tokens, the parameter is the URL we will be redirected to 
$request_token = $twitteroauth->getRequestToken('localhost.com/…'); 

// Saving them into the session 
$_SESSION['oauth_token'] = $request_token['oauth_token']; 
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

// If everything goes well.. 
if($twitteroauth->http_code==200){ 
// Let's generate the URL and redirect 
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']); header('Location: '. $url); } else { // It's a bad idea to kill the script, but we've got to know when there's an error. 
die('Something wrong happened.'); 
} 

感謝を使用していることを取得するには、ドキュメントに記載されたいずれかの手順を参照してくださいしないでください!

答えて

0

全プログラム(私は私のtwitterOAuth libには、あなたと同じであると信じて)

<?php 
/** 
* twitter_go.php -- users opens it and gets redirected to twitter.com 
*/ 

    require (dirname(__FILE__) . '/twoauth/twitterOAuth.php'); 

    $consumer_key = '123abcd'; 
    $consumer_secret = '1234567890'; 



    $to = new TwitterOAuth($consumer_key, $consumer_secret); 

    $tok = $to->getRequestToken(); 

    $token = $tok['oauth_token']; 
    $secret = $tok['oauth_token_secret']; 
    $time = $_SERVER['REQUEST_TIME']; 

    //temporary things, i will need them in next function, so i put them in cookie 
    setcookie("ttok", $token, $time + 3600 * 30, '/'); //,'.domain.com'); //migh need to add domain if got problems 
    setcookie("tsec", $secret, $time + 3600 * 30, '/'); //,'.domain.com'); 

    $request_link = $to->getAuthorizeURL($token); 

    //die($request_link); 
    header('Location: ' . $request_link); 


?> 

<?php 
    /** 
    * twitter_back.php -- users gets redirected here from twitter (if user allowed you app) 
    * you can specify this url in https://dev.twitter.com/ 
    */ 

     require (dirname(__FILE__) . '/twoauth/twitterOAuth.php'); 

     $consumer_key = '123abcd'; 
     $consumer_secret = '1234567890'; 


     $oauth_token = $_GET['oauth_token'] // http://domain.com/twitter_back.php?oauth_token=MQZFhVRAP6jjsJdTunRYPXoPFzsXXKK0mQS3SxhNXZI&oauth_verifier=A5tYHnAsbxf3DBinZ1dZEj0hPgVdQ6vvjBJYg5UdJI 

     if(!isset($_COOKIE['ttok'])) { 
      die('no cookie, no party'); 
     } 
     $ttok = $_COOKIE['ttok']; 
     $tsec = $_COOKIE['tsec']; 


     $to = new TwitterOAuth($consumer_key, $consumer_secret, $ttok, $tsec); 
     $tok = $to->getAccessToken(); 
     if(!isset($tok['oauth_token'])) { 
      die('try again!'); 
     } 
     $btok = $tok['oauth_token']; 
     $bsec = $tok['oauth_token_secret']; 



     $to = new TwitterOAuth($consumer_key, $consumer_secret, $btok, $bsec); 
     $content = $to->OAuthRequest('http://twitter.com/account/verify_credentials.xml'); 
     $user = simplexml_load_string($content); 
     $userid = $user->id . ''; //typecast to string 
     $screen_name = $user->screen_name . ''; 



     //delete temp. cookies 
     setcookie("ttok", '', 0, '/'); 
     setcookie("tsec", '', 0, '/'); 


     /** 
     * at this point you have everything on this user 
     */ 
     print_r($user); 
?> 
+0

:これは、このコードはやるん上記code.Whatに代わるものですか? –

+0

それは代替ではありません - それは2番目のステップです。あなたのコードはtwitter.comに行くユーザで終わり、twitter.comから戻ってきたユーザで始まります – izeed

+0

私のocdeとあなたのコードを混同することはできますか?あなたのコードと混同しますか?あなたのコードはどこに置くべきですか?$ _COOKIE [ ttok ']。あなたのコードを別のファイルに置くべきか、何が紛らわしいのでしょうか。 –

関連する問題