我有以下结构的应用程序
我正在使用rest 客户端库 https://github.com/philsturgeon/codeigniter-restclient连接到 MyAPI 并使用 php api 客户端 http://code.google.com/p/google-api-php-client/连接到 Google API
我的 Controller 代码如下
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 代码如下,确保用户存在于我的数据库中并通过谷歌进行身份验证
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);
}
}
问题是
当我通过直接 url 连接这个浏览器时
http://localhost/hac_dc/api/auth/user/ahmed.samy.cs@gmail.com
我得到了完美的 JSON 响应
但是当我使用 rest 客户端连接它时
我得到响应 false
我尝试改变我使用休息客户端的方式 我尝试添加第三个参数作为 JSON
和 MIME application/json
但没有成功
我不知道通过我的 REST API 连接另一个 REST API 是否有问题或不好的做法,
拉我的头发好几个小时了,请帮帮我
最佳答案
您的 REST 客户端不处理 oauth 身份验证,对吗? 我假设您应该以某种方式对您的 REST 客户端进行身份验证,以让它使用 Google 提供的访问 token 。
要手动执行此操作,您可以在手动访问 API(例如使用浏览器并检查浏览器 session )时将从 Google 收到的访问 token 保存到数据存储区。有了这个访问 token ,您就可以为您的 REST 客户端恢复一个有效的 session 。
向 Google 验证您的 REST 客户端,可以使用 cURL 或重复使用一些现有库(例如 https://github.com/philsturgeon/codeigniter-oauth2.git)来完成.要针对您自己的 API 对您的 REST 客户端进行身份验证,您可以在向您的 api 添加身份验证额外层(如果缺少)之后使用 HTTP 基本/摘要身份验证(如 CodeIgniter-REST 客户端所建议的那样)。
附言验证您的用户时,如果失败,401 响应状态代码可能更合法(另请参阅 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 )
关于php - 通过我的 REST API 连接 Google REST API 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854698/