谷歌语音 PHP OAuth 2.0

10

我曾经使用一个PHP类库连接到Google Voice发送短信。调用将会像这样工作:

$gv = new GoogleVoice("GmailAccount", "GmailPassword");
$gv->sms("PhoneNumber", "TextMsg");

直到2015年4月20日,它一直运行得很完美。但是谷歌停止支持旧的登录方法后,我的脚本就出现了500错误。谷歌表示必须使用OAuth 2.0进行身份验证,然而我没有在网上找到任何使用Google Voice实现此目的的示例。下面是代码,这不是我写的,请告诉我如何调整代码以使用Google的OAuth系统。

/*
Version     0.2
License     This code is released under the MIT Open Source License. Feel     free to do whatever you want with it.
Author      lostleon@gmail.com, http://www.lostleon.com/
LastUpdate  05/28/2010


Usage:


*/

class GoogleVoice
{
    public $username;
    public $password;
    public $status;
    private $lastURL;
    private $login_auth;
    private $inboxURL = 'https://www.google.com/voice/m/';
    private $loginURL = 'https://www.google.com/accounts/ClientLogin';
    private $smsURL = 'https://www.google.com/voice/m/sendsms';

public function __construct($username, $password)
{
    $this->username = $username;
    $this->password = $password;
}

public function getLoginAuth()
{
    $login_param = "accountType=GOOGLE&Email={$this->username}&Passwd={$this->password}&service=grandcentral&source=com.lostleon.GoogleVoiceTool";
    $ch = curl_init($this->loginURL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20");
    curl_setopt($ch, CURLOPT_REFERER, $this->lastURL);
    curl_setopt($ch, CURLOPT_POST, "application/x-www-form-urlencoded");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $login_param);
    $html = curl_exec($ch);
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    $this->login_auth = $this->match('/Auth=([A-z0-9_-]+)/', $html, 1);
    return $this->login_auth;
}

public function get_rnr_se()
{
    $this->getLoginAuth();
    $ch = curl_init($this->inboxURL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $html = curl_exec($ch);
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    $_rnr_se = $this->match('!<input.*?name="_rnr_se".*?value="(.*?)"!ms', $html, 1);
    return $_rnr_se;
}

public function sms($to_phonenumber, $smstxt)
{
    $_rnr_se = $this->get_rnr_se();
    $sms_param = "id=&c=&number=".urlencode($to_phonenumber)."&smstext=".urlencode($smstxt)."&_rnr_se=".urlencode($_rnr_se);
    $ch = curl_init($this->smsURL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_REFERER, $this->lastURL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sms_param);      
    $this->status = curl_exec($ch);
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    return $this->status;
}

private function match($regex, $str, $out_ary = 0)
{
    return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false;
}
}

切换到Oauth2,移除登录和密码相关内容。 - Linda Lawton - DaImTo
似乎不像简单地转移到Oauth2那样容易。在使用Oath2时,找不到Googlevoice的API("scope"变量所需)。欢迎提供额外的建议。 - F C
2个回答

5
我将把你带到下一个答案:https://dev59.com/GVHTa4cB1Zd3GeqPUL1h#4131915 不幸的是,谷歌语音已经改变了API,所以你不能再使用它了。参考https://github.com/aaronpk/Google-Voice-PHP-API(查看页首中的评论)。
Google Voice不是开放的API,因此他们不再维护它。很抱歉告诉你,在我自己的经验中,今天短信服务是如此便宜,以至于实际购买服务许可证费用要比与谷歌和他们不断变化的API作斗争少得多,你的网站总是会因为这样的变化而停机。认真思考一下你的时间作为一种资源,花费时间的代价更高!

1

GV4J是一个Java库,能够登录Google Voice,因此它可能是更新您的PHP代码以进行身份验证的良好参考。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接