Google语音PHP OAuth 2.0 [英] Google voice PHP OAuth 2.0

查看:122
本文介绍了Google语音PHP OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用一个PHP类库连接到Google语音来发送SMS文本消息.该呼叫将像这样工作:

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


直到最近为止,它一直没有问题,直到2015年4月20日,Google才停止支持登录到Google帐户的旧方法.因此我的脚本停止工作,并给出500错误. Google说您必须使用OAuth 2.0进行身份验证,但是我还没有在线找到有关如何使用Google语音实现此功能的示例.以下是代码,我尚未编写此代码,请让我知道如何调整代码以使用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;
}
}

解决方案

我将向您发送下一个答案: https://stackoverflow.com/a/4131915/2992810

不幸的是,谷歌语音已更改其API,因此您无法再使用它. https://github.com/aaronpk/Google-Voice-PHP-API(请查看标题中的评论)

Google语音不是开放的API,因此他们没有对其进行维护. 不好意思告诉您,但以我今天的经验,SMS服务是如此便宜,以至于您实际购买服务许可证的费用要比与Google竞争以及它们不断变化的API花费少,因此您的网站总是会因为此类更改而崩溃. 将您的时间视为一种资源,花时间会花费更多!

I once used a PHP Class Library to connect to Google Voice to send SMS Text Messages. The call would work something like this:

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


It worked flawlessly until recently, as of 4/20/2015, Google stopped supporting old methods of logging in to Google account. So my script stopped working giving 500 error. Google says you have to use OAuth 2.0 to authenticate however I haven't found any examples online on how to accomplish this with Google Voice. The code is below, I have not written this, please let me know how to adjust the code to use Google's OAuth System.

/*
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;
}
}

解决方案

I will send you to the next answer: https://stackoverflow.com/a/4131915/2992810

unfortunate google voice had changed their API so you can not use it anymore. https://github.com/aaronpk/Google-Voice-PHP-API (look at the comments in the head)

Google Voice is not an open API, so they are no maintaining it. Sorry to tell you but in my own experience today SMS services are so cheap that it will cost you less to actually buy service license than fight with Google and their constant API changes, your website can always go down because of such change. Thinks about your time as a resource, spending your time will cost you more!

这篇关于Google语音PHP OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆