【问题标题】:Sending Google API OAuth2 response to Controller [closed]向控制器发送 Google API OAuth2 响应 [关闭]
【发布时间】:2013-01-26 21:05:37
【问题描述】:

我是 CodeIgniter 和 PHP 的新手。 Google API 控制台中给出的重定向 URL:

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

以下代码向我显示了“请求权限”页面。当我点击Allow Access 时,它会带我到

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

我想将它重定向到另一个页面并在那里显示响应。控制器方法

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

}

查看实施

<?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/>

【问题讨论】:

    标签: php codeigniter google-api codeigniter-2


    【解决方案1】:

    为什么你的视图中有所有的 php 代码?这不是 MVC 的工作方式。

    逻辑代码应该保存在您的控制器中,而不是您的视图中。我建议您使用 Codeiginter OAuth2 库,因为来自 Google 的 OAuth2 库的原始代码并不意味着与 Codeiginter 或任何其他特定的 MVC 框架兼容。例如你可以使用这个:https://github.com/philsturgeon/codeigniter-oauth2

    您被重定向到正确的控制器,因为此处设置了 url:

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

    还可以看看 Codeigniter 的重定向方法: redirect('somecontroller/some_method')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2015-12-28
      相关资源
      最近更新 更多