2017-02-03 24 views
1

私はGoogleアプリケーションを更新しています(Googleディレクトリサービスを使用して)。アカウントの作成に問題はありませんが、これまではGoogle Gmailサービスを使用してsendAsEmail属性を設定することができませんでした(エイリアスが「From」に表示されるように)。また、以下のスニペットのvar_dump($ createSendAsResult)は出力を生成しません。どんな助けもありがとう。ありがとう! は、ここに私のコードです:GoogleのGmail API経由でsendAsEmailを設定する方法

//Create account in Google             
function createGoogleAccount($server_name, $acc_user, $acc_password)                           
{                                             
    $clientDir = getClientDir($server_name);                                   
    $dirService = new Google_Service_Directory($clientDir);                                

    $userInstance = new Google_Service_Directory_User();                                
    $nameInstance = new Google_Service_Directory_UserName();                               

    $nameInstance -> setGivenName('Generic');                                   
    $nameInstance -> setFamilyName($acc_user);                                   

    $userInstance -> setOrgUnitPath("/generic_email");                                 
    $userInstance -> setName($nameInstance);                                   
    $userInstance -> setHashFunction("MD5");                                   
    $domain = getDomain($server_name);                                     
    $primary_email = $acc_user . '@' . $domain;                                   
    $userInstance -> setPrimaryEmail($primary_email);                                 
    $userInstance -> setPassword(hash("md5", $acc_password));                               
    $optParams = array();                                        

    $error_msg = null;                                         
    try                                             
    {                                             
      $createUserResult = $dirService->users->insert($userInstance, $optParams);                         
      var_dump($createUserResult);                                    
    }                                             
    catch (Google_IO_Exception $gioe)                                     
    {                                             
      $error_msg = "Error in connection: ".$gioe->getMessage();                             
    }                                             
    catch (Google_Service_Exception $gse)                                    
    {                                             
      $error_msg = "Service Exception: ".$gse->getMessage();                              
    }                                             

    addSendAs($server_name, $acc_user, $domain);                          return $error_msg;                                         
}                                               
function addSendAs($server_name, $acc_user, $domain)                                  
{                                               
    $clientGmail = getClientGmail($server_name);                                  
    $gmailService = new Google_Service_Gmail($clientGmail);                                
    $primary_email = $acc_user . '@' . $domain;                                   
    $sendAsEmail = new Google_Service_Gmail_SendAs();                                 

    $alias = '';                                          
    if (($server_name == null) || (strpos($server_name, "dev") != false))                            
    {                                             
      $alias = '@g.';                                        
    }                                             
    else                                            
    {                                             
      $alias = '@mail.';                                       
    }                                             

    $sendAsEmail -> setSendAsEmail($acc_user . $alias . $domain);                              
    $sendAsEmail -> setIsDefault(TRUE);                                     
    $sendAsEmail -> setIsPrimary(TRUE);                                     

    $error_msg = null;                                         
    try                                             
    {                                             
      $createSendAsResult = $gmailService->users_settings_sendAs -> create($primary_email, $sendAsEmail);                   
      var_dump($createSendAsResult);                                    
    }                                             
    catch (Google_IO_Exception $gioe)                                     
    {                                             
      $error_msg = "Error in connection: ".$gioe->getMessage();                             
    }                                             
    catch (Google_Service_Exception $gse)                                    
    {                                             
      $error_msg = "Service Exception: ".$gse->getMessage();                              
    }                                                                       
}  
+0

Gmail APIのTry it部分を使ってこの 'sendAsEmail'属性を設定しようとすると、エラー403'ドメイン全体の権限を委任されたサービスアカウントに限定したアクセス "が表示されました。この[スレッド](https://productforums.google.com/forum/#!topic/apps/a185jwNyDlM)で、このエラーの意味は、「初期アカウントには管理者またはスーパー管理者の委任が必要ですコントロールパネルでは、アカウントの追加権限(委任)を付与します。 – KENdi

+0

私の最初の投稿以来、例外メッセージを出力するためのerror_log()ステートメントの追加を含むいくつかのコード変更を行いました。 (私のプログラミング経験の大部分はJavaで書かれていますが、このプロジェクトにはPHPを使用する必要がありますので、まだ慣れています)このアカウントにはドメイン全体の承認があり、アクセススコープにはGmailのすべてが含まれています。サービスの例外:{\ n "エラー":{\ n "エラー":[\ n {\ n "ドメイン": "グローバル"、\ n "理由": "禁止"、\ n " メッセージ": "代表団が[email protected]を拒否しました" \ n} \ n]、\ n ... –

答えて

0

は最後に、コードや同僚のカップルからいくつかの助けを借りて、多くの実験の後、私は問題が何であるか、それを解決する方法を考え出しました。

明らかに、Googleはメールで新しいユーザーのアカウントを設定する必要があります。私が10秒間の遅延、つまりsleep(10)をPHPで追加したとき、SendAsエイリアスの作成を含めて、アカウントがGmail APIへの呼び出しの準備が整っていれば十分でした。

+0

実生活の場合と同様に、解決策はスリープです。 :) –

関連する問題