连接我的 REST API 到 Google REST API 不起作用。

5

我有一个以下结构的应用程序 enter image description here

我正在使用 rest client library https://github.com/philsturgeon/codeigniter-restclient 连接到 MyAPI, 并且使用 php api client http://code.google.com/p/google-api-php-client/ 连接到 Google API

我的控制器代码如下

 function index()
    {
        if($this->form_validation->run())
        {
           $logged = $this->rest->get('auth/user',array(
               'email'=>$this->input->post('email')
                   )); 
           var_dump($logged);
        }
        $this->load->view('layout/login',$this->data);
    }

这是我的API代码,用于处理该请求。它确保用户在我的数据库中存在,并且已通过Google进行了身份验证。

function user_get()
    {
        $response=NULL;
        $data=array(
            'email'=>$this->get('email')
        );
        $google_account=$this->google->authenticate();
        if(  isset($google_account) && $this->user_model->login($data))
        {
            $response->status='success';
            $response->message=$google_account;
        }
        else
        {
            $response->status='error';
            $response->message='Failed to authenticate user';   
        }
        $this->response($response,200);
    }

Google 库函数 `Authenticate' 的代码如下:

function authenticate()
    {
        $oauth2 = new Google_Oauth2Service($this->client);
        if (isset($_GET['code']))
        {
            $this->client->authenticate($_GET['code']);
            $_SESSION['token'] = $this->client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
            return;
        }
        if (isset($_SESSION['token'])) 
        {
            $this->client->setAccessToken($_SESSION['token']);
        }
        if (isset($_REQUEST['logout'])) 
        {
            unset($_SESSION['token']);
            $this->client->revokeToken();
        }
        if ($this->client->getAccessToken()) 
        {
            $user = $oauth2->userinfo->get();
            // The access token may have been updated lazily.
            $_SESSION['token'] = $this->client->getAccessToken();
            return $user;
        } 
        else 
        {
            $authUrl = $this->client->createAuthUrl();
            redirect($authUrl);
        }

    }

问题是 当我通过直接网址连接时:http://localhost/hac_dc/api/auth/user/ahmed.samy.cs@gmail.com,我可以完美地获取JSON响应。

但是当我使用rest客户端进行连接时:

我得到的响应为false。我已经尝试了改变我的rest客户端的使用方式。我尝试将第三个参数作为JSON和MIME application/json添加,但仍然没有解决这个问题。

我不知道通过我的REST API连接另一个REST API是问题还是不好的做法,我已经为此烦恼了数小时,请帮帮我。


你一直在使用什么样的REST客户端? - Thierry M.S.
https://github.com/philsturgeon/codeigniter-restclient - Ahmed Gaber
2个回答

5
您的REST客户端是否不处理OAuth身份验证?我假设您需要对REST客户端进行身份验证,以便让它使用Google提供的访问令牌。手动完成此操作时,您可以在手动访问API(例如使用浏览器并检查浏览器会话)时保存从Google接收到的访问令牌到数据存储中。有了此访问令牌,您就可以为REST客户端恢复有效的会话。向Google进行REST客户端身份验证可以使用cURL或重新使用某些现有库,例如https://github.com/philsturgeon/codeigniter-oauth2.git。要对自己的API进行REST客户端身份验证,可以在添加身份验证额外层之后使用HTTP基本/摘要身份验证(如CodeIgniter-REST客户端建议的那样)。

P.S. 在验证用户时,如果失败,401响应状态码可能更合适(另请参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2


我正在使用REST客户端连接我的API,但是当连接到Google时,我使用支持OAuth 2.0的Google API。 - Ahmed Gaber
你的应用程序(嵌入API)是否使用Google的OAuth API进行身份验证?在使用rest客户端连接到自己的API时,你是否检查了应用程序的身份验证状态?也许你可以提供一个终端点来通知你的REST客户端有关你的应用程序对Google API的OAuth身份验证状态,以确保完全覆盖此问题。 - Thierry M.S.
是的,我正在使用OAuth和Google API可以从CI控制器正常连接,但无法从Rest API控制器连接。 - Ahmed Gaber
所以我担心您可能需要从当前的REST客户端切换到另一个支持OAauth身份验证的客户端。 - Thierry M.S.

0

问题是跨域请求错误。您可以连接

localhost/hac_dc/api/auth/user/ahmed.samy.cs@gmail.com

因为您的服务器充当Web代理,所以无法直接使用简单的getJSON技术进行REST。您可以在jQuery Ajax中使用JSONP,它对跨域非常有效。JSONP不是JSON,它返回带有JSON参数的回调方法的js函数调用。您可以通过以下Ajax调用实现。但是,在JSONP中没有“成功”回调,为此,您必须在Ajax调用中提供回调参数并定义该回调函数。

$.ajax({
       crossDomain: true,
       type:"GET",
       contentType: "text; charset=utf-8",
       url: YOUR_URL + "&callback=mycallback",
        jsonp:'jsonp',
       dataType: "jsonp"
       });
function mycallback(result){
    //your callback operation
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接