簡體   English   中英

Twitter API v1.1重定向到應用程序不起作用

[英]Twitter API v1.1 redirect to application does not work

我是Twitter API的新手,我正在嘗試在應用程序中獲取用戶時間軸數據。

我已經正確設置了應用程序設置和config.php文件,因此用戶成功登錄並看到“重定向回您的應用程序”消息,但是重定向到http://MyDomainName.com/callback.php?oauth_token=someToken&oauth_verifier = someOtherToken頁面不會顯示在瀏覽器中,並且不會發生重定向。 手動返回到應用程序頁面,該用戶尚未登錄,必須再次登錄。 誰能幫我這個忙嗎?

Config.php

/**
 * @file
 * A single location to store configuration.
 */

define('CONSUMER_KEY', 'ALPHA_NUMERIC_CONSUMER_KEY');
define('CONSUMER_SECRET', 'ALPHA_NUMERIC_SECRET');
define('OAUTH_CALLBACK', "http://infosys.concordia.ca/MyApps/oauthProxy/callback.php");
define('OAUTH_COOKIE', 'my_twitter_app_oauth');
define('OAUTH_COOKIE_DOMAIN', '.concordia.ca'); //Example ".esri.com"
echo OAUTH_CALLBACK;

//REQUIRED - Encrypt your cookies
//http://si0.twimg.com/images/dev/oauth_diagram.png
//Create your own unique ENCRYPTION_KEY via Encrypt.get_RandomKey()
define('ENCRYPTION_KEY','MY_UNIQUE_ENCRYPTION_KEY'); 
//Create your own unique initialization vector via Encrypt.get_IV()
define('IV','MY_UNIQUE_IV');
define('DEFAULT_TIME_ZONE','America/Toronto');

Callabck.php

<?php

//Version 2.1 by AndyG 4/2013
//Changes
//- added OAuth Encrption

// Start session and load lib
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('twitteroauth/Encrypt.php');
require_once('config.php');

$content = null;    //for verification of credentials
$connection = null; //for getting access token

// check if cookie exists
if(isset($_COOKIE[OAUTH_COOKIE])){
    // redirect back to app
    if(isset($_SESSION['oauth_referrer'])){
        header('Location: '.$_SESSION['oauth_referrer']);
        exit;
    }
}
else{
    // if verifier set
    if(isset($_REQUEST['oauth_verifier'])){

        //Best practice is to encrypt the cookies or not use cookies
        $key = base64_decode(ENCRYPTION_KEY);
        $iv = base64_decode(IV);
        $encrypt = new Encrypt($key,$iv,DEFAULT_TIME_ZONE);


        // Create TwitteroAuth object with app key/secret and token key/secret from default phase
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

        // get access token from twitter
        try{
            $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
        }
        catch(Exception $e){
            header("HTTP/1.0 400 Error");
            echo "\n\nFailed retrieving access token: " .$e->getMessage();
            exit;
        }

        //Add a credentials validation request. Added v2.0 by AndyG
        try{
            $content = $connection->get('account/verify_credentials','');
        }
        catch(Exception $e){
            $error = $e->getMessage();
        }
        // save token
        $_SESSION['oauth_access_token'] = $access_token;
        // 1 year
        $cookie_life = time() + 31536000;

        if($content != null && $content->screen_name != ""){

            $token = base64_encode( $encrypt->encrypt($access_token['oauth_token']));
            $token_secret = base64_encode( $encrypt->encrypt($access_token['oauth_token_secret']));     

            //Update array with new encrypted values
            $access_token["oauth_token"] = $token;
            $access_token["oauth_token_secret"] = $token_secret;
            // echo "\n\n".var_dump($access_token); //for testing

            // set cookie
            setcookie(OAUTH_COOKIE, json_encode($access_token), $cookie_life, '/', OAUTH_COOKIE_DOMAIN);
            //header('Location: ./callback.php');
            echo "<html><head><title>Valid Verification</title><body bgcolor='#C0C0C0'>";
            echo "<style type='text/css'>body{font-family:sans-serif;}</style>";
            echo "<table width='100%'><tr bgcolor='#FFFFFF'><td>";
            echo "<a href='http://www.esri.com'><img src='edn.png' style='border-style:none' alt='ESRI Developer Network' /></a>";
            echo "</td></tr></table>";
            echo "<h2>Welcome:&nbsp;&nbsp;<img src='".$content->profile_image_url."'></img>&nbsp;&nbsp;&nbsp;@".$content->screen_name."</h2>";
            echo "<h4>You have successfully authenticated with Twitter. </h4>" ;
            echo "<h4>It is okay to close this page and return to the application.</h4>";
            echo "<script language=\"JavaScript\">\n";
            echo "if(window.opener && window.opener.getTokens){";
            echo "window.opener.getTokens(\"".$_SESSION['oauth_token'].",".$_SESSION['oauth_token_secret']."\");}";
            //You can also have the app automatically close the window via self.close, as shown below
            //echo "self.close();";
            echo "</script>";
            echo "</body></html>";
        }
        else{
            header("HTTP/1.0 400 Error");
            echo "\n\nFailed to validate credentials. ".$error;
            exit;
        }
        exit;
    }
    else{
       // redirect
        if(isset($_SESSION['oauth_referrer'])){
            header('Location: '.$_SESSION['oauth_referrer']);
        }
        else{
            header('Location: '.OAUTH_CALLBACK);
        }
        exit;
    }
}

沒有任何代碼,很難看到您做錯了什么。

在獲取初始請求令牌時,您必須將標准URL傳遞為oauth_callback ,或者將回調URL硬編碼到Twitter應用程序設置中

如果您傳入了oauth_callback,我相信您必須在設置中輸入一個虛擬值,否則它將不起作用。

我的錯! 應用程序設置中的回調URL與config.php中的回調URL不同。 現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM