使用 Google OAuth 进行身份验证

Authenticating with Google OAuth

提问人:RobertPitt 提问时间:8/4/2011 最后编辑:RobertPitt 更新时间:8/26/2011 访问量:1496

问:

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

我正在使用 Codeigniter 2.0.2,但我的服务器上没有编译 PECL OAuth,所以我必须使用原始 php 来做到这一点。

我甚至已经用谷歌对用户进行了身份验证,并取回了该用户的oauth_tokenoauth_token_secret

但是在无休止地阅读协议之后,我仍然找不到该oauth_keyoauth_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

php oauth protected-resource

评论

0赞 danp 4/12/2012
还有问题吗?

答:

2赞 Dave White 8/26/2011 #1

您是否尝试过尝试使用 Google OAuth Playground ?它有助于确定您可以访问哪些 API 源并构建有效的请求字符串。

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

希望能有所帮助