【问题标题】:Google voice PHP OAuth 2.0谷歌语音 PHP OAuth 2.0
【发布时间】:2015-08-08 22:02:12
【问题描述】:

我曾经使用一个 PHP 类库连接到 Google Voice 以发送 SMS 文本消息。该调用将像这样工作:

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

直到最近,它一直完美运行,截至 2015 年 4 月 20 日,Google 停止支持旧的登录 Google 帐户的方法。所以我的脚本停止工作,给出 500 错误。 Google 说您必须使用 OAuth 2.0 进行身份验证,但是我没有在网上找到任何关于如何使用 Google Voice 完成此操作的示例。代码如下,我没有写这个,请告诉我如何调整代码以使用谷歌的 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,去掉登录名和密码的东西。
  • 看起来不像迁移到 Oauth2 那样简单。找不到 Googlevoice 的 API(使用 Oath2 时“范围”变量需要)。欢迎提供其他建议

标签: php oauth oauth-2.0 google-oauth google-voice


【解决方案1】:

我会送你到下一个答案: https://stackoverflow.com/a/4131915/2992810

不幸的是,谷歌语音已经改变了他们的 API,所以你不能再使用它了。 https://github.com/aaronpk/Google-Voice-PHP-API(看头上的cmets)

Google Voice 不是一个开放的 API,因此他们不维护它。 很遗憾地告诉你,但根据我自己的经验,今天的 SMS 服务非常便宜,实际上购买服务许可证的成本比与谷歌及其不断的 API 变化抗争要少,你的网站总是会因为这种变化而崩溃。 将您的时间视为一种资源,花费您的时间会花费您更多!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2013-05-07
      • 2015-02-14
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多