使用Google OAuth进行身份验证

时间:2011-08-04 15:45:34

标签: php oauth protected-resource

我目前正在为我的网站开发OAuth身份验证系统,该系统允许用户通过Google API登录。

我正在使用Codeigniter 2.0.2并且我没有在我的服务器上编译PECL OAuth,因此我必须使用原始php来执行此操作。

我已经开始使用Google对用户进行身份验证,并为该用户取回 oauth_token oauth_token_secret

但是在无休止地阅读协议后,我仍然无法找到 oauth_key oauth_token_secret 如何访问其他API以收集以下信息:

  • 电子邮件地址
  • 个人资料图片
  • 姓名
  • 位置

以下是我用于验证的当前代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OAuth extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('google_oauth',array(
            'key'       => '*REMOVED*',
            'secret'    => '*REMOVED*',
            'algorithm' => 'HMAC-SHA1'
        ));
    }

    public function login()
    {
        //Get Request Token
        $response = $this->google_oauth->get_request_token(
            site_url("/oauth/collect"),
            'http://www.google.com/m8/feeds/'
        );

        //Store temp
        $this->session->set_flashdata('g.token.secret', $response['token_secret']);

        //Redirect
        redirect($response['redirect']);
    }

    public function collect()
    {
        $token_s = $this->session->flashdata('g.token.secret');
        if(!$token_s)
        {
            $this->session->set_flashdata('error','Invalid auth session data, please rety your login');
            redirect();
        }

        $oauth = $this->google_oauth->get_access_token(false, $token_s);

        /*Check the data is valid*/
        if(!isset($oauth['oauth_token']) || !isset($oauth['oauth_token_secret']))
        {
            $this->session->set_flashdata('error','Unable to authecticate securly with Google, please try again');
            redirect();
        }

        $token = $oauth['oauth_token'];
        $secret = $oauth['oauth_token_secret'];

        //Will Store Here
    }
}

我目前正在使用此身份验证类的略微修改版本:

https://github.com/jimdoescode/CodeIgniter-YouTube-API-Library/blob/master/application/libraries/google_oauth.php

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用Google OAuth Playground进行试验?它可用于计算您可以访问的API源并构建有效的请求字符串。

以下是您可以访问的Google API列表:Google API Directory

希望有所帮助