顯示具有 email 標籤的文章。 顯示所有文章
顯示具有 email 標籤的文章。 顯示所有文章

2019年6月18日 星期二

Google API 筆記 - 使用 Google OAuth 2.0 與 IdToken 和 People API 取得 Email address

大概在 2015 年開發的 Google OAuth2 登入流程 已經需要更新了。當初一直靠 Google Plus API 取得用戶的 Email (GMail) 資訊,結果 Google+ APIs 已經在 2019/03/07 領便當了
https://developers.google.com/+/integrations-shutdown
停用 Google+ 整合服務
2019 年 3 月 7 日起,所有網站上與行動應用程式中的 Google+ 整合功能將全部停止運作。詳細情形如下:
網路整合 (例如外掛程式與互動訊息) 將停止提供服務。如果網站擁有者未採取任何行動,網站的版面配置和/或功能可能會受到影響。
行動應用程式整合 (例如 +1 按鈕、分享到 Google+ 與應用程式活動) 將停止運作。
我們將從 1 月下旬起逐漸停用相關功能。最早從 2019 年 1 月 29 日起,網站與行動應用程式整合功能會偶爾發生失敗的情形。
我們強烈建議開發人員儘快從其網站上和/或行動應用程式中移除相關程式碼。我們也將陸續停用 Google+ API 和 Google+ 登入功能。詳情請參閱這份額外
通知。
雖然我們即將停用 Google+ 一般使用者版本,但我們同時致力為企業機構 提供 Google+ 服務。Google+ 即將換上全新風貌並加入新功能,如需詳細資訊, 請參閱這篇網誌文章
原先是靠這 Google Plus API 取得 Email address:

<?php
$profile_ret = @json_decode(file_get_contents('https://www.googleapis.com/plus/v1/people/me?'.http_build_query( array(
'access_token' => $access_token,
))), true);


現在就改靠 People API:https://developers.google.com/people/api/rest/v1/people/get

<?php
$profile_ret =  @json_decode(file_get_contents('https://people.googleapis.com/v1/people/me?'.http_build_query( array(
'personFields' => 'names,nicknames,emailAddresses,coverPhotos,photos',
'access_token' => $access_token,
))), true);


在 2019 年 06 月,這隻 Google Plus api : https://www.googleapis.com/plus/v1/people/me 仍正常工作著,說不定它就真的是一直從 People API 服務著。

除此之外,原先 Google OAuth2 已經推薦改用 IdToken 進行認證,若是走 IdToken 時,可以在當下就拿到 Email Address 了:

<?php

require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setClientId($oauth_project_client_id);
$client->setClientSecret($oauth_project_secret_key);
$client->setAccessType("offline");
$client->addScope(['email','profile']);
$client->setRedirectUri($oauth_callback_url);

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

$uid = $token_data['sub'];
$email = isset($token_data['email_verified']) && $token_data['email_verified'] ?$token_data['email'] : NULL;


如此就搞定取得用戶 Email address 的項目了!

其他筆記:

$ curl -s 'https://people.googleapis.com/v1/people/me?personFields=names%2Cnicknames%2CemailAddresses%2CcoverPhotos&access_token=USER_ACCESS_TOKEN'
{
  "resourceName": "people/UID",
  "etag": "XXXXXXXXXXXX",
  "names": [
    {
      "metadata": {
        "primary": true,
        "source": {
          "type": "PROFILE",
          "id": "UID"
        }
      },
      "displayName": "NICKNAME",
      "familyName": "LAST_NAME",
      "givenName": "FIRST_NAME",
      "displayNameLastFirst": "LAST_NAME, FIRST_NAME"
    }
  ],
  "coverPhotos": [
    {
      "metadata": {
        "primary": true,
        "source": {
          "type": "PROFILE",
          "id": "UID"
        }
      },
      "url": "https://XXX.googleusercontent.com/cXXXXXXX",
      "default": true
    }
  ],
  "emailAddresses": [
    {
      "metadata": {
        "primary": true,
        "verified": true,
        "source": {
          "type": "ACCOUNT",
          "id": "UID"
        }
      },
      "value": "USER_EMAIL@gmail.com"
    }
  ]
}

2016年1月14日 星期四

[Perl] 測試及修正 Perl 5.10.0 的 Email:Send 支援 SMTP + TLS + AUTH 寄信方式 @ perlbrew 5.10.0 / Ubuntu 14.04

上次一摸 Perl 有點久了 Orz 這個問題主要是發生在舊版 Bugzilla 3.0.5 on Perl 5.10.0 環境上,想要走 SMTP + TLS + AUTH 的寄信方式,只是核心部分抽出來驗證後,只須如此就能打造測試環境了:

$ sudo apt-get install perlbrew libssl-dev
$ perlbrew init
$ perlbrew --notest install 5.10.0
$ perlbrew user 5.10.0
bash-4.3$ perl -v

This is perl, v5.10.0 built for x86_64-linux

Copyright 1987-2007, Larry Wall

bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'TimeDate')"
bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'Date::Format')"
bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'Email::Address')"
bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'Email::MIME')"
bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'Email::Send')"
bash-4.3$ perl -MCPAN -e "CPAN::Shell->notest('install', 'Net::SMTP::TLS')"


如此一來,就可以寫簡單的 Perl 程式測試寄信,此例是處理騰訊企業郵箱的寄信方式:

bash-4.3$ vim send.pl
#!/usr/bin/perl

# http://search.cpan.org/dist/Email-Send/lib/Email/Send.pm
use Date::Format qw(time2str);
use Encode::MIME::Header;
use Email::Address;
use Email::MIME;
use Email::MIME::Modifier;
use Email::Send;
use MIME::Base64;

push @args,
        #Host  => 'hwsmtp.exmail.qq.com:465',
        #Host  => 'smtp.exmail.qq.com:465',
        #Host  => 'smtp.exmail.qq.com:587',
        #Host  => 'smtp.exmail.qq.com',
        Host  => 'hwsmtp.exmail.qq.com',
        username => 'account@my.domainname',
        password => 'password_for_account',
        Hello => 'localhost',
        #ssl => 1,
        tls => 1,
        Debug => 1;

$body = "Hello World";
$method = 'SMTP';
$from = 'account@my.domainname';
$to = 'account@my.domainname';
$subject = "Test Mail";

my $email = ref($msg) ? $msg : Email::MIME->new($body);
$email->header_set('MIME-Version', '1.0') if !$email->header('MIME-Version');
$email->header_set('From', $from);
$email->header_set('To', $to);
$email->header_set('Date', time2str("%a, %e %b %Y %T %z", time()));
$email->header_set('Subject', $subject);
foreach my $part ($email->parts) {
        $part->charset_set('UTF-8');
        #$part->encoding_set('quoted-printable') if !is_7bit_clean($part->body);
}

my $mailer = Email::Send->new({ mailer => $method, mailer_args => \@args });
my $retval = $mailer->send($email);

print $retval;


執行看看:

bash-4.3$ perl send.pl
invalid SSL_version specified at /home/ubuntu/perl5/perlbrew/perls/perl-5.10.0/lib/site_perl/5.10.0/IO/Socket/SSL.pm line 568


人工修正:

bash-4.3$ vim /home/ubuntu/perl5/perlbrew/perls/perl-5.10.0/lib/site_perl/5.10.0/IO/Socket/SSL.pm
將 2210行註解舊的,並更新成新的 Regular Expressiong
#m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1(?:_?[12])?))$}i
m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))}i


再次執行:

bash-4.3$ perl test/send.pl
Message sent


因此,這只是單純 Perl 5.10.0 的 SSL.pm 的 TLS 規則影響,造成使用騰訊企業郵箱時,透過 SMTP + TLS + AUTH 寄信時無法成功。而這篇的目的只是記錄透過 Perl Email::Send 寄信走認證的筆記 XD 另外, Email::Send 走 SSL + AUTH 一直沒成功過 Orz

2015年5月22日 星期五

Google API 筆記 - 使用 Google OAuth 2.0 API @ Ubuntu 14.04, PHP 5.5


準備替服務整合 Google Plus 登入方式,因此研究了一下 Google OAuth 2.0 API 的用法:
  • 在 Google Developer Console 建立一個 Google Project
  • 開啟此 Google Project 的 OAuth 使用,並設置"重新導向 URI",此處 URI 雖然不支援萬用符號,但支援輸入多筆,還算夠用。
整個過程下來,得到 client_id、client_secret 兩筆資料,如此一來就可以進行 OAuth 使用:

先組出 Google OAuth 所需的網址提供使用者授權,需要填寫 scope, response_type, redirect_uri, access_type, approval_prompt, client_id,例如:

$init_url = 'https://accounts.google.com/o/oauth2/auth?'. http_build_query( array(
'scope' => 'email profile',
'response_type' => 'code',
'redirect_uri' => $google_oauth_callback_url,
'access_type' => 'offline',
'approval_prompt' => 'force',
'client_id' => $google_project_client_id,
));
header('Location: '.$init_url);


接著,在 callback_url 的網頁中,接了 Google OAuth 回傳的 code 後,從 server site 發 requests 進行處理:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v3/token');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array(
'client_id' => $google_project_client_id,
'client_secret' => $google_project_secret_key,
'code' => $google_ret_code,
'grant_type' => 'authorization_code',
'redirect_uri' => $google_oauth_callback_url,
)));
$ret = @json_decode(curl_exec($ch), true);


若一切順利的話,在 $ret 中,就可以拿到 access_token 了(此例需開啟 Google+ API)

$profile = @json_decode(file_get_contents('https://www.googleapis.com/plus/v1/people/me?'.http_build_query( array(
'access_token' => $ret['access_token']
))), true);

print_r($profile);


最後,若測試完後,想要替自己用的 Google account 刪除 app 授權的話,可以這邊刪除:

https://security.google.com/settings/security/permissions?pli=1

2014年11月12日 星期三

[Linux] 使用 awk, sort, uniq, grep, dig 批次驗證 email address @ Ubuntu 14.04

工作時中,偶爾會對 UNIX 系統感動! 雖然有時很簡單的任務會龜毛到硬要連續指令來處理,明明寫一小段 code 就好了啊 Orz

取處已知清單中的 email domain:

$ mysql -e "select email from db.user;" > email.log;
$ sort email.log | uniq | awk -F'@' '{print $2}' | sort | uniq | grep "." > email.domain.log


最後的 grep "." 則是要避開 hostname 不合法的項目(如 localhost 等)。

驗證 mx:

$ dig -t mx test.com +short | wc -l
$ test `dig -t mx test.com +short | wc -l` -ne 0 && echo "ok"


批次驗證:

$ awk '{ system("test `dig -t mx "$1" +short | wc -l` -ne 0 && echo "$1)}' email.domain.log > email.validated.log

2014年8月25日 星期一

[PHP] Mantis Email Notification Settings @ Ubuntu 12.04

在 Ubuntu 12.04 透過 apt-get 安裝後,配置好系統管理後,又手動把它生成 Mantis 1.2.17 了。預設 Mantis 是有啟動 Email Notification,包含新增帳號後,還要由對方 email 去設定密碼等。對於 Email Notification 的設定,如事件通知的寄信者資訊等,都是直接更改 /path/mantis/config_inc.php。

可以參考 http://www.mantisbt.org/manual/admin.config.email.htmlhttp://www.mantisbt.org/manual/admin.customize.email.html,簡易筆記:

//$g_enable_email_notification = OFF;
$g_mail_receive_own = OFF;
$g_default_notify_flags = array(
'reporter' => ON,
'handler' => ON,
'monitor' => ON,
'bugnotes' => ON,
'threshold_min' => NOBODY,
'threshold_max' => NOBODY
);

//
// Select the method to mail by: PHPMAILER_METHOD_MAIL for use of mail() function, PHPMAILER_METHOD_SENDMAIL for sendmail (or postfix), PHPMAILER_METHOD_SMTP for SMTP. Default is PHPMAILER_METHOD_MAIL.
//
$g_phpMailer_method = PHPMAILER_METHOD_MAIL;
$g_smtp_host = 'localhost';
$g_smtp_username = '';
$g_smtp_password = '';
$g_administrator_email = 'admin@example.com';
$g_from_email = 'noreply@example.com';
$g_from_name = 'Mantis Bug Tracker';

2013年11月1日 星期五

使用 mutt 產生 message/rfc822 測資 @ Ubuntu 12.04

最近正在研究 email 格式,其中一個 content-type: message/rfc822 很神秘,好像很久沒看過了?甚至 2010 年 GMail 相關討論串 中,仍看得到抱怨 GMail 不處理 (現況經測試,GMail 都會將 message/rfc822 展開)

接著就是來產生 message/rfc822 了,這似乎比較常從 outlook 那邊看到,但懶得用outlook 產品,直接用 mutt 吧!

使用 mutt 時,可以設定 mime forward 機制後,接著每一封轉信都可以用 message/rfc822 格式寄出。

$ vim ~/.muttrc
set mime_forward=yes
set mime_forward_rest=yes


或是使用 mutt 時,按 : 去設定上述兩個值亦可。

一封寄給自己的信:

Return-Path: <changyy@vm>
X-Original-To: changyy@vm
Delivered-To: changyy@vm
Received: by vm (Postfix, from userid 1000)
        id 03701604E5; Sat, 26 Oct 2013 10:53:47 +0800 (CST)
Date: Sat, 26 Oct 2013 10:53:47 +0800
From: changyy <changyy@vm>
To: changyy <changyy@vm>
Subject: Test message/rfc822
Message-ID: <20131026025347.GA17760@vm>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.5.21 (2010-09-15)
Status: RO
Content-Length: 9
Lines: 1

as title


把上述那封用 message/rfc822 轉寄給自己:

Return-Path: <changyy@vm>
X-Original-To: changyy@vm
Delivered-To: changyy@vm
Received: by vm (Postfix, from userid 1000)
        id B96A9604E5; Sat, 26 Oct 2013 10:54:13 +0800 (CST)
Date: Sat, 26 Oct 2013 10:54:13 +0800
From: changyy <changyy@vm>
To: changyy <changyy@vm>
Subject: [changyy@vm: Test message/rfc822]
Message-ID: <20131026025413.GB17760@vm>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="KsGdsel6WgEHnImy"
Content-Disposition: inline
User-Agent: Mutt/1.5.21 (2010-09-15)
Status: RO
Content-Length: 703
Lines: 29


--KsGdsel6WgEHnImy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

forward test

--KsGdsel6WgEHnImy
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <changyy@vm>
X-Original-To: changyy@vm
Delivered-To: changyy@vm
Received: by vm (Postfix, from userid 1000)
        id 03701604E5; Sat, 26 Oct 2013 10:53:47 +0800 (CST)
Date: Sat, 26 Oct 2013 10:53:47 +0800
From: changyy <changyy@vm>
To: changyy <changyy@vm>
Subject: Test message/rfc822
Message-ID: <20131026025347.GA17760@vm>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.5.21 (2010-09-15)

as title

--KsGdsel6WgEHnImy--