2017-08-25 143 views
0

我正在開發一個PHP,它使用Gmail API從Gmail中讀取電子郵件。Gmail PHP API獲取錯誤的帳戶電子郵件

基本上我正在按照快速入門頁面做的(https://developers.google.com/gmail/api/quickstart/php)。

所以,首先我加載客戶端:

require __DIR__ . '/Google/vendor/autoload.php'; 
$client = new Google_Client(); 
$client->setAuthConfigFile(__DIR__ . '/client_secret.json'); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt('force'); 
$client->setScopes(Google_Service_Gmail::MAIL_GOOGLE_COM); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php'); 

然後,如果我加載使用的用戶ID,從我的應用程序存儲在數據庫中的憑據。 如果憑據AR不存在我做的重定向的文檔賽斯:

$client->setAccessType('offline'); 
    $client->setApprovalPrompt('force'); 
    $auth_url = $client->createAuthUrl(); 
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

授權我得到使用由谷歌在我的重定向URL提供的代碼的訪問令牌後:

$token = $client->fetchAccessTokenWithAuthCode($_GET['code']); 
//storing the data in the db 
header('Location: /index.php'); 

中當然,如果令牌過期我在驗證過程中,刷新:

if($client->isAccessTokenExpired()) { 
    $refreshToken = $client->getRefreshToken(); 
    $client->fetchAccessTokenWithRefreshToken($refreshToken); 
    $newToken = $client->getAccessToken(); 
    $newToken['refresh_token'] = $refreshToken; 
    //store the new token 
} 

所以在此之後我可以使用用戶的電子郵件列表,它完美地工作。 問題是,如果我使用我的帳戶登錄,我會看到正確的電子郵件,但是在使用另一個用戶登錄另一個帳戶後,我看到兩個帳戶都有相同的電子郵件。

因此,例如,如果我爲用戶1授權帳戶[email protected],則對用戶2授權郵件[email protected],在這兩種情況下,我都會看到相同的電子郵件(來自smith @ gmail。 COM)。

我檢查過,如果問題是我的,所以如果加載的訪問令牌是相同的,但不幸的是,這種訪問令牌是不同的。

另一個瘋狂的事情是,如果我刷新第二個帳戶([email protected])的令牌,我會看到正確的電子郵件,但現在seth會看到史密斯電子郵件。

這是怎麼回事?我想念什麼?

UPDATE:

我試過每一步,問題是訪問令牌,我已經試過了也是在另一臺服務器,但都返回相同的電子郵件。

我也試圖與[email protected]和手動我檢索到的訪問令牌來獲取授權碼:

$token = $client->fetchAccessTokenWithAuthCode($code); 

,但沒辦法,也這樣做,它會不斷驗證我作爲其他電子郵件。

UPDATE:

事情越來越陌生,我試圖刪除配置文件的所有部分,我用它只有當我需要檢索授權碼。

所以在請求中,我只傳遞訪問令牌數組,所發生的是我可以看到電子郵件也完全移除訪問令牌,所以基本上客戶端在請求時看起來是空的。

(
[auth:Google_Client:private] => 
[http:Google_Client:private] => 
[cache:Google_Client:private] => 
[token:Google_Client:private] => Array 
    (
     [access_token] => no token what so eva 
    ) 

[config:Google_Client:private] => Array 
    (
     [application_name] => 
     [base_path] => https://www.googleapis.com 
     [client_id] => 
     [client_secret] => 
     [redirect_uri] => 
     [state] => 
     [developer_key] => 
     [use_application_default_credentials] => 
     [signing_key] => 
     [signing_algorithm] => 
     [subject] => 
     [hd] => 
     [prompt] => 
     [openid.realm] => 
     [include_granted_scopes] => 
     [login_hint] => 
     [request_visible_actions] => 
     [access_type] => online 
     [approval_prompt] => auto 
     [retry] => Array 
      (
      ) 

    ) 

[logger:Google_Client:private] => 
[deferExecution:Google_Client:private] => 
[requestedScopes:protected] => Array 
    (
    ) 

+0

您是否嘗試過使用不同訪問令牌的google oauth遊樂場,以查看它是否會得到相同的結果? – Talus

+0

謝謝你的回答,是的,我現在已經試過了,他們工作正常,女性都顯示正確的電子郵件列表。 但是,如果在PHP腳本中它們都返回相同的電子郵件列表,所以問題出現在腳本中,但與快速入門中的問題相同,您能否看一下? (https://pastebin.com/SyqGrVQq) – Dtnand

+0

我忘了的另一個細節,在我發佈的腳本中,如果我檢查郵件列表是來自「[email protected]」的郵件列表,如果我刪除訪問類型,範圍和審批提示行,它會顯示「[email protected]」電子郵件。 – Dtnand

回答

1

我通過更新客戶端庫到最新版本解決了這個。

相關問題