-1

開発サーバーに google plus api を実装しましたが、正常に動作します。本番サーバーで同じコードを使用しました。しかし、許可を要求した後、自分のサイトに戻ってログインするのに時間がかかります。

何が原因なのか誰か教えてください。私はoauth2を使用しました。

以下は私が使用しているコードです

<?php
session_start();
require_once 'googleplus/src/Google_Client.php';
require_once 'googleplus/src/contrib/Google_Oauth2Service.php';

class Webpage_UserGPlusLogin extends Webpage
{
    public function __construct()
    {
        $temp_redirect = $_SESSION['RETURN_URL_AFTERLOGIN'];
        $this->title = 'User Account';

        $client = new Google_Client();
        $client->setApplicationName(WEBSITE_NAME); 
        $client->setClientId(GOOGLE_PLUS_CLIENT_ID); // Client Id
        $client->setClientSecret(GOOGLE_PLUS_CLIENT_SECRET); // Client Secret
        $client->setRedirectUri(GOOGLE_PLUS_REDIRECT_URI); // Redirect Uri set while creating API account
        $client->setDeveloperKey(GOOGLE_PLUS_DEVELOPER_KEY); // Developer Key
        $oauth2 = new Google_Oauth2Service($client);


        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['token'] = $client->getAccessToken();
            $redirect = GOOGLE_PLUS_REDIRECT_URI;
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); // Redirects to same page
            return;
        }

        if (isset($_SESSION['token'])) {
            $client->setAccessToken($_SESSION['token']);
        }

        if (isset($_REQUEST['logout'])) {
            unset($_SESSION['token']);
            $client->revokeToken();
        }

        if(!isset($_SESSION['email_address_user_account'])) // Check if user is already logged in or not
        {
            if ($client->getAccessToken()) {
                $user = $oauth2->userinfo->get();  // Google API call to get current user information

                $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
                $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
                $googleuserid = $user['id'];
                $given_name = $user['given_name'];
                $family_name = $user['family_name'];
                // The access token may have been updated lazily.
                $_SESSION['token'] = $client->getAccessToken();

                // If email address is present in DB return user data else insert user info in DB
                $this->result = UserAccount::gplus_sign_up($email, $googleuserid, $given_name, $family_name); 

                // Create new user object.
                $this->user_account = new UserAccount($this->result['id'],$this->result['email_address'],$this->result['password'],$this->result['confirmation_code'],$this->result['is_confirmed'], $this->result['first_name'], $this->result['last_name']); 

                $_SESSION['gplus_email_address'] = $email;
                $_SESSION['gplus_first_name'] = $given_name;
                $_SESSION['gplus_last_name'] = $family_name;
                $_SESSION['gplus_id'] = $googleuserid;
                $_SESSION['gplus_profile_pic'] = $img;
                $_SESSION['email_address_user_account'] = $email;

            } else {
                $authUrl = $client->createAuthUrl();
            }
        }

        if(isset($temp_redirect))
            header("Location:".$temp_redirect);
        else
            header("Location:/");
    }
}

前もって感謝します

4

1 に答える 1

1

これを試して

次のコードを使用

$temp = json_decode($_SESSION['token']);
$request = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";                
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$request);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization: OAuth '.$temp->access_token));
$response = trim(curl_exec($curl));
$info =  curl_getinfo($curl);
$request_header_info = curl_getinfo($curl, CURLINFO_HEADER_OUT);

//var_dump($info);
//var_dump($request_header_info);           
curl_close($curl);
echo "<pre>";
print_r(json_decode($response));    

代わりに

$user = $oauth2->userinfo->get();  // Google API call to get current user information`enter code here`

これがあなたを助けることを願っています.. :)

于 2013-03-21T07:44:40.137 に答える