Php 如何通过Google设置sendAsEmail';s Gmail api

Php 如何通过Google设置sendAsEmail';s Gmail api,php,gmail,gmail-api,Php,Gmail,Gmail Api,我正在更新一个PHP应用程序,以便在Google中创建新的电子邮件帐户(使用Google目录服务)。我创建帐户没有问题,但到目前为止,我无法使用Google Gmail服务设置sendAsEmail属性(这样别名将显示在“发件人”中)。此外,下面代码段中的var_dump($createSendAsResult)不会产生任何输出。任何帮助都将不胜感激。谢谢 这是我的密码: //Create account in Google

我正在更新一个PHP应用程序,以便在Google中创建新的电子邮件帐户(使用Google目录服务)。我创建帐户没有问题,但到目前为止,我无法使用Google Gmail服务设置sendAsEmail属性(这样别名将显示在“发件人”中)。此外,下面代码段中的var_dump($createSendAsResult)不会产生任何输出。任何帮助都将不胜感激。谢谢 这是我的密码:

//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();                                                                                                                     
    }                                                                                                                                                                                                                                                                                          
}      

最后,在对代码进行了大量的实验和几位同事的帮助后,我找到了问题所在以及解决方法


显然,谷歌需要时间来建立一个新用户的电子邮件帐户。当我添加一个10秒的延迟(php中的sleep(10))时,这个延迟足以让帐户准备好调用Gmail API,包括创建SendAs别名。

好吧,如果我尝试使用Gmail API的try it部分设置这个
sendAsEmail
属性,我会收到一个错误403
“访问受限于已被授予域范围权限的服务帐户”
我在这篇文章中发现,此错误的含义是“初始帐户必须具有管理员或超级管理员授权才能在管理员控制面板中操作,然后在该面板中授予帐户添加权限(授权)。“自从我发表第一篇文章以来,我做了一些代码更改,包括添加了一个error_log()语句来输出异常消息。(我的大部分编程经验都是用Java编写的,但我需要在这个项目中使用php,所以我还是习惯了。)这个帐户有域范围的授权,访问范围包括Gmail的所有内容。以下是我得到的错误:服务异常:{\n“错误”:{\n“错误”:[\n{\n“域”:“全局”、\n“原因”:“禁止”、\n“消息”:“针对泛型\u测试的委派被拒绝”_svc@sandbox.xxx.edu“\n}\n],\n…就像在现实生活中一样,解决方案是睡眠。:)