2015-05-27 15 views
10

Ich habe einmal eine PHP-Klassenbibliothek verwendet, um eine Verbindung zu Google Voice herzustellen, um SMS-Textnachrichten zu senden. Der Anruf würde wie folgt funktionieren:Google voice PHP OAuth 2.0

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

Es funktionierte einwandfrei, bis vor kurzem, als von 2015.04.20, stoppte Google alte Methoden der Anmeldung bei Google-Konto zu unterstützen. So hat mein Skript aufgehört, zu arbeiten, 500 Fehler gebend. Google sagt, dass Sie OAuth 2.0 verwenden müssen, um sich zu authentifizieren. Ich habe jedoch keine Beispiele gefunden, wie dies mit Google Voice erreicht werden kann. Der Code ist unten, ich habe dies nicht geschrieben, bitte lassen Sie mich wissen, wie Sie den Code anpassen, um das OAuth-System von Google zu verwenden.

/* 
Version  0.2 
License  This code is released under the MIT Open Source License. Feel  free to do whatever you want with it. 
Author  [email protected], 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; 
} 
} 
+0

Wechseln Sie zu Oauth2, entfernen Sie das Login und Passwort Zeug. – DaImTo

+0

Es scheint nicht so einfach zu sein, wie zu Oauth2 zu wechseln. Eine API für Googlevoice kann nicht gefunden werden (erforderlich für die Variable "scope" bei Verwendung von Oath2). Zusätzliche Beratung ist willkommen –

Antwort

5

Ich werde dich auf die nächste Antwort senden: https://stackoverflow.com/a/4131915/2992810

unglückliche google Stimme hatte sich verändert ihre API, so dass Sie es nicht mehr verwenden können. https://github.com/aaronpk/Google-Voice-PHP-API (siehe die Kommentare im Kopf)

Google Voice ist keine offene API, so dass sie nicht gepflegt werden. Tut mir leid, Ihnen zu sagen, aber in meiner eigenen Erfahrung heute SMS-Dienste sind so billig, dass es weniger kostet, tatsächlich Service-Lizenz zu kaufen als kämpfen mit Google und ihre konstanten API-Änderungen, Ihre Website kann immer wegen solcher Änderung gehen. Denke über deine Zeit als Ressource nach, deine Zeit wird dich mehr kosten!

0

GV4J ist eine Java-Bibliothek, die sich bei Google Voice anmelden kann. Daher könnte es eine gute Referenz für die Aktualisierung Ihres PHP-Codes sein, um sich authentifizieren zu können.