duanmao1319 2013-01-26 21:05
浏览 48
已采纳

向Controller发送Google API OAuth2响应[关闭]

I am new to CodeIgniter and PHP. Redirect URL given in Google API Console:

Redirect URIs:  http://localhost/testsaav/index.php/main/gmail_invite

Following code shows me Request For Permission page. When I click Allow Access it takes me to

http://localhost/testsaav/index.php/main/gmail_invite?code=4/CXD462cen-oEBe1GaHIH90hjqb2X.QpVsg7mG4AUXaDn_6y0ZQNgaCVLxeAI

I want to redirect it to another page and show the response there. Controller method

function gmail_invites($data)
{
   $this->load->view('socialInvites2',$data);

}

View implementation

<?php 
            require_once APPPATH.'libraries/Google_Client.php';
            session_start();
            $client = new Google_Client();
            $client->setApplicationName('Google Contacts PHP Sample');
            $client->setScopes("http://www.google.com/m8/feeds/");
            $client->setClientId('xxx.apps.googleusercontent.com');
            $client->setClientSecret('xxx-xxx');
            $client->setRedirectUri('http://localhost/testsaav/index.php/main/gmail_invite');

            if (isset($_GET['code'])) {
              $client->authenticate();
              $_SESSION['token'] = $client->getAccessToken();
              $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
              header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
            }

            if (isset($_SESSION['token'])) {
             $client->setAccessToken($_SESSION['token']);
            }

            if (isset($_REQUEST['logout'])) {
              unset($_SESSION['token']);
              $client->revokeToken();
            }

            if ($client->getAccessToken()) {
              $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
              $val = $client->getIo()->authenticatedRequest($req);
              //$xml = simplexml_load_string($val->getResponseBody());
              //$result = $xml->xpath('//gd:email');

              /*foreach ($result as $title) {
                echo $title->attributes()->address . "<br>";
              }*/
              // The contacts api only returns XML responses.
              $response = json_encode(simplexml_load_string($val->getResponseBody()));
              //print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

              // The access token may have been updated lazily.
              $_SESSION['token'] = $client->getAccessToken();
            } else {
              $auth = $client->createAuthUrl();
            }

            print "<a class='facebook-button' id='facebookbutton' href='$auth'>
            <span class='fb-button-left'></span>
            <span class='fb-button-center'>Invite GMail Friends</span>
            <span class='fb-button-right'></span></a>"; ?>
            <br/>
  • 写回答

1条回答

  • drrhr20884 2013-01-27 00:02
    关注

    Why do you have all that php code in your view? That's not how MVC works.

    Logic code should be kept in your Controller and not your View. I suggest that you use a Codeiginter OAuth2 library because the original code from Google's OAuth2 lib isn't meant to be compatible with Codeiginter or any other particular MVC framework. For example you could use this one: https://github.com/philsturgeon/codeigniter-oauth2

    You're being redirected to the correct controller because the url is set here:

    $client->setRedirectUri('http://localhost/testsaav/index.php/main/gmail_invite');
    

    Also take a look at Codeigniter's redirect method: redirect('somecontroller/some_method')

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)