Google应用引擎imap无效凭据

时间:2014-05-16 02:47:48

标签: google-app-engine

我试图收到用户的电子邮件。我有一个服务帐户设置和来自客户端库的访问令牌。我将这些详细信息输入到https://developers.google.com/gmail/xoauth2_libraries中找到的xoauth库中。但我一直收到无效的凭据错误。一个帐户工作,其管理/模拟帐户,但没有其他工作。这是一个PHP应用程序可以有人请帮助。

$client = new Google_Client();
$client -> setApplicationName("TTS Dashboard");
$client -> setClientId(CLIENTID);
$client -> setClientSecret(CLIENTSECRET);

$oauth2 = new Google_Service_Oauth2($client);
$directoryService = new Google_Service_Directory($client);
$driveService = new Google_Service_Drive($client);
$calendarService = new Google_Service_Calendar($client);

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

$path = 'http://' . $_SERVER['HTTP_HOST'] . "/static/" . KEYFILEPATH;
$key = file_get_contents($path);
$cred = new Google_Auth_AssertionCredentials(SERVICEACCOUNTEMAIL, $scopes, $key);
$cred -> sub = $impersonatedEmailAddress;

$client -> setAssertionCredentials($cred);

$client -> getAuth() -> refreshTokenWithAssertion($cred);

if ($client -> getAuth() -> isAccessTokenExpired()) {
    $client -> getAuth() -> refreshTokenWithAssertion($cred);
}

$_SESSION['service_token'] = $client -> getAccessToken();

function GetAccessTokenString() {
    $accessToken = $_SESSION['service_token'];
    $token = json_decode($accessToken, true);
    $tokenString = $token['access_token'];
    return $tokenString;
}
// Get access token string value
$tokenString = GetAccessTokenString();
tryImapLogin($studentEmail, $tokenString);

1 个答案:

答案 0 :(得分:0)

使用服务帐户和域范围委派,您需要为每个用户创建一个访问令牌,即您需要依次模拟每个用户。

管理员访问令牌不允许您访问其他用户的邮箱。