如何将Google Plus OAuth API与Codeigniter集成?

时间:2017-06-11 13:58:37

标签: php codeigniter google-oauth2

我正在尝试构建一个简单的项目,其中将保存google plus配置文件数据或将其更新到数据库中。我无法找到解决无效字符URI问题的方法。我收到了来自谷歌的回复无效的字符URI。

如何解决URI无效的问题?

我在这篇文章末尾附上了错误的图片。

登录控制器类代码为:

 public function index() {


      // Include the google api php libraries
    include_once APPPATH."libraries/google-api-php-client/Google_Client.php";
    include_once APPPATH."libraries/google-api-php-client/contrib/Google_Oauth2Service.php";

    // Google Project API Credentials
    $clientId = 'My Client ID';
    $clientSecret = 'My Secret here';
    $redirectUrl = 'https://example.com/index.php?login/index/';

    // Google Client Configuration
    $gClient = new Google_Client();
    $gClient->setApplicationName('Login to Application');
    $gClient->setClientId($clientId);
    $gClient->setClientSecret($clientSecret);
    $gClient->setRedirectUri($redirectUrl);
    $google_oauthV2 = new Google_Oauth2Service($gClient);

    if (isset($_REQUEST['code'])) {
        $gClient->authenticate();
        $this->session->set_userdata('token', $gClient->getAccessToken());
        redirect($redirectUrl);
    }

    $token = $this->session->userdata('token');
    if (!empty($token)) {
        $gClient->setAccessToken($token);
    }

    if ($gClient->getAccessToken()) {
        $userProfile = $google_oauthV2->userinfo->get();
        // Preparing data for database insertion
        $userData['oauth_provider'] = 'google';
        $userData['oauth_uid'] = $userProfile['id'];
        $userData['first_name'] = $userProfile['given_name'];
        $userData['last_name'] = $userProfile['family_name'];
        $userData['email'] = $userProfile['email'];
        $userData['gender'] = $userProfile['gender'];
        $userData['locale'] = $userProfile['locale'];
        $userData['profile_url'] = $userProfile['link'];     
        $userData['picture_url'] = $userProfile['picture'];
        // Insert or update user data
        $userID = $this->user->checkUser($userData);
        if(!empty($userID)){
            $data['userData'] = $userData;
            $this->session->set_userdata('userData',$userData);
        } else {
           $data['userData'] = array();
        }
    } else {
        $data['authUrl'] = $gClient->createAuthUrl();
    }
    $this->load->view('backend/login',$data);
}

错误图像
enter image description here

我无法解决这个问题。请建议我如何避免这种情况。

0 个答案:

没有答案