dongmeng9048 2017-02-07 02:25
浏览 46

如何从Google API PHP获取电子邮件地址

I am trying to figure out how to use the Gmail API to get the email address from a message, but it seems like when the data is sent, the actual email address get's escaped out of existence, leaving only the person's name. Here is an example of a response that I get that should have the user's email address (Formatted as PHP Array):

 Array ( [0] => Array ( [name] => To [value] => John Smith ) ) 

But, when I try it out on the Gmail API Documentation page, I get a response like this (Formatted as JSON):

"headers": [
   {
    "name": "To",
    "value": "John Smith \u003cjohnsmith@test.com\u003e"
   }
  ]

Now, I'm not sure what is going on, but it seems that the backslashes and everything in between them, including the email address that I need, is being escaped before I even have a chance at getting to it. Is there some way to avoid this so that I can get the email address from the response? Here is the current code that I am using now:

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



$client_email = "myclientemail";
$private_key = file_get_contents('myprivatekey');
$scopes = array('https://www.googleapis.com/auth/gmail.modify');
$userId = 'myuserid';
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key,
    'notasecret',                                 // Default P12 password
    'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
    $userId
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Gmail($client);

$opt_param = array(
    'format'=>'metadata',
    'metadataHeaders' => 'To'
  );

    $message = $service->users_messages->get($userId, "messageid", $opt_param);
print_r($message);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 slam rangenet++配置
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊