使用Google Open ID Connect访问用户的Gmail帐户

时间:2016-07-30 00:23:03

标签: google-apps-script oauth-2.0 google-oauth openid google-openid

如何在多个Gmail帐户上运行Google Apps脚本API?我目前有一个脚本,当它被下面显示的两个文件授权时访问用户的gmail。一旦代码请求授权,我就会为客户端存储刷新令牌。但是,如何使用Open ID获取各种用户的授权。账户?除了刷新令牌之外,我是否还需要存储客户端ID才能获得用户gmail帐户的授权?感谢您抽出时间提前帮助!

read_email.php

<?php
    require_once '../google-api-php-client/src/Google/autoload.php';

    function getEmails (){
        session_start();

        $client = new Google_Client();
        $client->setAuthConfigFile('client_secrets.json');
        $client->setScopes(array(
        'https://mail.google.com/'
        ));

        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
            $client->setAccessToken($_SESSION['access_token']);
            if($client->isAccessTokenExpired()){
                header('Location: http://' . $_SERVER['HTTP_HOST'] . '/email_database/php/oauth2callback.php');
            }

            // Get the API client and construct the service object.
            $service = new Google_Service_Script($client);
            $scriptId = '**********************';

            // Create an execution request object.
            $request = new Google_Service_Script_ExecutionRequest();
            set_time_limit(0);
            $request->setFunction('test');
            $response = $service->scripts->run($scriptId, $request);
            $response = $response->getResponse();
            $response = $response['result'];
            return ($response);


        } else {
            $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/email_database/php/oauth2callback.php';
            header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
        }

    }

    ?> 

oauth2callback.php

<?php
require_once '../google-api-php-client/src/Google/autoload.php';
include 'functions.php';

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/email_database/dashboard/php/oauth2callback.php');
$client->setScopes(array(
'https://mail.google.com/'
));
$client->setAccessType('offline');
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/email_database/dashboard/php/read_email.php';
$username = $_SESSION['username'];
$user = getUser($username);
if($user['refresh_token'] == null){
    if (! isset($_GET['code'])) {
        $auth_url = $client->createAuthUrl();
        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    } else {
        //echo $_GET['code'];
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
        $google_token = json_decode($_SESSION['access_token']);
        print_r($google_token);
        $refresh_token = $google_token->refresh_token;
        addRefreshToken($username,$refresh_token);
        //header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }

}
else {
    $refresh_token = $user['refresh_token'];
    $client->refreshToken($refresh_token);
    $_SESSION['access_token']= $client->getAccessToken();
   // header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

?>

0 个答案:

没有答案