duanlu0559 2014-01-30 06:24
浏览 72
已采纳

执行twitter登录(服务器端)并通过jquery / ajax获取登录结果

In login.php I have html, ajax code. Login button executes php script redirect.php to perform twitter authentication.

redirect.php invokes someother files for authentication, and finally gives result I want: id and name

I want to retrieve this value in login.php file.

Login.php

<script type = "text/javascript">
    $('#loginTwitter').click(function () {
        window.location.href = 'redirect.php';
        //After executing, it should come back here to fetch `name` and `id`
        $.get('redirect.php', function(data) {
           var  user_id= data.id;
           var name = data.name;
            //post name and id to start.php
            alert(user_id);
            $.post('start.php', { user_id : user_id }, function ()
            {
                    window.location.href = 'start.php';
            });
        },"json");
    });
   </script>

redirect.php

<?php

/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);

/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL and redirect user to Twitter. */
    $url = $connection->getAuthorizeURL($token);
//      echo "URL is : $url";
//    header('Location: ' . $url);

        if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
            header('Location: ./clearsessions.php');
        }
        $access_token = $_SESSION['access_token'];
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
        $content = $connection->get('account/verify_credentials');
        $twitteruser = $content->{'screen_name'};
        $notweets = 5;
        $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
        $name = $content->{'name'};
        $id = $content->{'id'};
        echo json_encode((object) array('id' => $id, 'name' => $name));


    break;
  default:
    /* Show notification if something went wrong. */
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
    ?>
  • 写回答

1条回答 默认 最新

  • dousha1873 2014-02-06 18:51
    关注

    twitterLoginForSite

    Add twitter login button on your site and get user profile data into db

    There are couple of files to do this in nice flow

    Get source here : GitHub link

    1. config.php - Add you twitter auth keys on this page. Later we can access those value just by including this file

    2. login.php - You site page. We will keep login button here

    3. redirect.php - This page will start session and load library function. It creates connection using your twitter keys and redirect to twitter authentication system. Here user authenticates your Twitter apps and enters his twitter credentials

    4. callback.php - After user enters correct parameters he is redirect to this page. If the oauth_token is old redirect to the connect page. If the user has been verified then the access tokens can be saved for future use. it will do it automatically. Here you can now get user profile info. But for easiness we will do it in next page index.php

    5. index.php - Get user profile info and store it in db

    Customization of Abrahams twitter auth github source.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog