通过我的REST API连接Google REST API无效

Ahm*_*amy 5 php api rest web-services codeigniter

我有这个应用程序具有以下结构 在此输入图像描述

我正在使用rest客户端库 https://github.com/philsturgeon/codeigniter-restclient 连接到MyAPI并使用php api客户端 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);
    }
Run Code Online (Sandbox Code Playgroud)

我处理此请求的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);
    }
Run Code Online (Sandbox Code Playgroud)

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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

问题是 当我通过浏览器与直接url连接时,我 http://localhost/hac_dc/api/auth/user/ahmed.samy.cs@gmail.com 完全得到JSON响应

但是当我使用rest客户端连接它时

我得到了回应false我尝试改变我使用我的休息客户端的方式我试图添加第三个参数JSON和作为MIME,application/json但没有工作

我不知道如果通过我的REST API连接另一个REST API是有问题或不好的做法,

我拉了几个小时的头发,请帮帮我

Thi*_*nne 5

您的REST客户端不处理oauth身份验证,是吗?我假设您将以某种方式验证您的REST客户端,以便它使用Google提供的访问令牌.

要手动执行此操作,您可以在手动访问API时(例如,使用浏览器和检查浏览器会话)将您从Google收到的访问令牌保存到数据存储区.有了此访问令牌,您就可以恢复REST客户端的有效会话.

可以使用cURL或重用某些现有库(例如https://github.com/philsturgeon/codeigniter-oauth2.git)来对Google的REST客户端进行身份验证.要向自己的API验证REST客户端,可以在向api添加身份验证附加层(如果缺少)后使用HTTP基本/摘要式身份验证(如CodeIgniter-REST客户端所示).

PS在验证用户身份时,如果失败,401响应状态代码可能更合法(另请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2)