ラベル OAuth の投稿を表示しています。 すべての投稿を表示
ラベル OAuth の投稿を表示しています。 すべての投稿を表示

2013年9月23日月曜日

Twitter Oauth 認証の流れ

1.    POST oauth/request_token
      https://api.twitter.com/oauth/request_token



2.    GET oauth/authorize
      https://api.twitter.com/oauth/authorize



3.   POST oauth/access_token
     https://api.twitter.com/oauth/access_token



例   http://hpapi.blogspot.jp/2013/09/post-oauthrequesttoken.html
      POST oauth/request_token 成功
      

2013年9月21日土曜日

POST oauth/access_token

POST oauth/access_token 成功

<?php
//error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
?>
<?php
//new newlogin(oauth_consumer_key, oauth_consumer_key_secret, Callback_URL);
$x=new newlogin('**********', '*************', 'http://www.dodofei.com/oauthget.php');
if(!array_key_exists('oauth_token',$_REQUEST) and !array_key_exists('oauth_verifier',$_REQUEST)){
$x->request_token();
}else{
$x->access_token("http://www.dodofei.com");
}

?>
<?php
class newlogin{

public $debug='off';
public $Request_token_URL='https://api.twitter.com/oauth/request_token';
public $Authorize_URL='https://api.twitter.com/oauth/authorize';
public $Access_token_URL='https://api.twitter.com/oauth/access_token';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';
public $oauth_para=array();
  public $oauth_consumer_key='';
public $oauth_consumer_key_secret= '';
public $oauth_token_secret= '';

public function newlogin($oauth_consumer_key, $oauth_consumer_key_secret, $Callback_URL){

$this->oauth_para['oauth_callback']=$Callback_URL;
$this->oauth_para['oauth_consumer_key']=$oauth_consumer_key;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_consumer_key_secret=$oauth_consumer_key_secret;
}

public function request_token(){
//request_token
$signature_key=rawurlencode($this->oauth_consumer_key_secret).'&';
$respons=$this->fun_curl($this->oauth_para, $signature_key, $this->Request_token_URL,'POST');
$requestToken = array();
foreach(explode('&',$respons) as $val){
$param = explode('=',$val);
$requestToken[$param[0]] = $param[1];
}
$this->oauth_token_secret=$requestToken['oauth_token_secret'];
header(sprintf('Location: %s?%s',$this->Authorize_URL,'oauth_token='.$requestToken['oauth_token']));
}

public function authorize(){
//authorize
header(sprintf('Location: %s?%s',$this->Authorize_URL,$respons));
}

public function access_token($hpurl){
//access_token
$signature_key=rawurlencode($this->oauth_consumer_key_secret).'&'.rawurlencode($this->oauth_token_secret);
$add_para=array();
$add_para['oauth_token']=$_GET['oauth_token'];
$add_para['oauth_verifier']=$_GET['oauth_verifier'];
$para=$this->oauth_para+$add_para;
$respons=$this->fun_curl($para, $signature_key, $this->Access_token_URL,'POST');

foreach (explode('&',$respons) as $val){
$param=explode('=',$val);
setcookie($param[0],$param[1],time()+60*60*24*30*30);
}

if($respons!='error'){
//header('Location: http://www.dodofei.com/index.php?'.$respons);
header('Location: '.$hpurl);
}
}



public function fun_curl($para, $signature_key, $url, $httptype){
//oauth_signature
ksort($para);
$temp=array();
foreach ($para as $key=>$val){
if(!empty($val)){
$temp[count($temp)]=$key.'='.rawurlencode($val);
}
}

$bas_sting=implode('&',$temp);
$signature_base_string=rawurlencode($httptype).'&'.rawurlencode($url).'&'.rawurlencode($bas_sting);

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($signature_base_string);
}

// NG $this->oauth_para['oauth_signature']=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true)));
$para['oauth_signature']=base64_encode(hash_hmac('sha1', $signature_base_string, $signature_key, true));

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($temp);
}

//Authorization: OAuth
ksort($para);
$temp1=array();
$temp2=array();

foreach ($para as $key=>$val){
if(!empty($val)){
$temp1[count($temp1)]=$key.'="'.urlencode($val).'"';
if ($key!='oauth_callback'){
$temp2[count($temp2)]=$key.'='.urlencode(rawurldecode($val));
}
}
}

$Authorization_header='Authorization: OAuth '.implode(', ',$temp1);

if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp1);
}

//GET利用しない
$request_curldata=implode('&',$temp2);
if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp2);
}


//CURL 送信
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($Authorization_header,'Content-Length:','Expect:','Content-Tpye:'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));

if($httptype=='GET'){
//GET動かない
$geturl=$url.'?'.$request_curldata;
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_curldata);
curl_setopt($ch, CURLOPT_URL, $geturl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
}

if($httptype=='POST'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
}

$response_josn = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
$curlgetinfo=curl_getinfo($ch);
curl_close($ch);


if($curlgetinfo['http_code']==200){
return $response_josn;
}else{
return 'error';
}
}

}


?>

2013年9月20日金曜日

GET oauth/authorize 成功

GET oauth/authorize 成功

<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
?>
<?php

$x=new newlogin('***************','********************','http://www.********.com');
//http://www.********.com/?oauth_token=z4MV1x5JnEyaRaq5VbpylGoZETAX6XVv5USpZJ247N8&oauth_verifier=5eFa14MMF5DWi8LK3ZsOsyMkg7WinBGwxXNvrNUqKhE
//http://www.********.com/?denied=7ejbFPRb1oZ1Z08r8XQOauRdIeh4EkLWKH0xNFI

?>
<?php


class newlogin{

public $debug='off';
public $request_type='POST';
public $oauth_para=array();
public $Request_token_URL='https://api.twitter.com/oauth/request_token';
public $Authorize_URL='https://api.twitter.com/oauth/authorize';
public $Access_token_URL='https://api.twitter.com/oauth/access_token';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';

public $Authorization_header;
public $signature_base_string;
public $oauth_signature;
public $signature_key;
public $curlgetinfo;
public $response_josn;
public $request_curldata;


public function newlogin($oauth_consumer_key,$oauth_consumer_key_secret,$Callback_URL){

$this->oauth_para['oauth_callback']=$Callback_URL;
$this->oauth_para['oauth_consumer_key']=$oauth_consumer_key;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_signature']='';
$this->signature_key=rawurlencode($oauth_consumer_key_secret).'&';

$this->oauth_set();
$this->send_curl();
}

public function oauth_set(){
//oauth_signature
ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp[count($temp)]=$key.'='.rawurlencode($val);
}
}

$bas_sting=implode('&',$temp);
$this->signature_base_string=rawurlencode($this->request_type).'&'.rawurlencode($this->Request_token_URL).'&'.rawurlencode($bas_sting);

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($this->signature_base_string);
}

// NG $this->oauth_para['oauth_signature']=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true)));
$this->oauth_para['oauth_signature']=base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true));
$this->oauth_signature=$this->oauth_para['oauth_signature'];

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($temp);
}

//Authorization: OAuth
ksort($this->oauth_para);
$temp1=array();
$temp2=array();

foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp1[count($temp1)]=$key.'="'.urlencode($val).'"';
if ($key!='oauth_callback'){
$temp2[count($temp2)]=$key.'='.urlencode(rawurldecode($val));
}
}
}

$this->Authorization_header='Authorization: OAuth '.implode(', ',$temp1);

if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp1);
}

//GET利用しない
$this->request_curldata=implode('&',$temp2);
if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp2);
}

}

public function send_curl(){
//CURL 送信
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header,'Content-Length:','Expect:','Content-Tpye:'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));

if($this->request_type=='GET'){
//GET動かない
$geturl=$this->Request_token_URL.'?'.$this->request_curldata;
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
curl_setopt($ch, CURLOPT_URL, $geturl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
}

if($this->request_type=='POST'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $this->Request_token_URL);
}

$this->response_josn = curl_exec($ch);
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
$this->curlgetinfo=curl_getinfo($ch);
curl_close($ch);

if($this->curlgetinfo['http_code']==200){
header(sprintf('Location: %s?%s',$this->Authorize_URL,$this->response_josn));
echo 'url Send';
}else{
sleep(3);
$this->oauth_set();
$this->send_curl();
echo 'url Error';
}
}



public function debug(){

//Debug
echo '<br>/////////Debut///////////////////';
echo '<br><pre>';
echo 'request_datas<br>';
//echo print_r($this->request_datas);
echo '<br><br>';
echo 'Signature base string<br>';
echo $this->signature_base_string;
echo '<br><br>';
echo 'oauth_signature<br>';
echo $this->oauth_signature;
echo '<br><br>';
echo 'Authorization header<br>';
echo $this->Authorization_header;
echo '<br><br>';
echo 'Curl data<br>';
echo $this->request_curldata;
echo '<br><br>';
echo 'Curl_getinfo<br>';
echo print_r($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo print_r($this->response_josn);
echo '<br><br>';
echo '/////////Debut///////////////////';

}


}





?>

2013年9月19日木曜日

POST oauth/request_token 成功

POST oauth/request_token
成功、次はGET oauth/authorize


<?php
date_default_timezone_set('Asia/Tokyo');

error_reporting(E_ALL);

?>

<?php

$x=new newlogin();
$x->oauth_set();
$x->send_curl();
$x->debug();



class newlogin{

public $debug='off';
public $request_type='POST';
public $oauth_para=array();
  public $oauth_consumer_key= '***********************';
public $oauth_consumer_key_secret= '******************************';
public $oauth_token= '';
public $oauth_token_secret= '';
public $Request_token_URL='https://api.twitter.com/oauth/request_token';
public $Authorize_URL='https://api.twitter.com/oauth/authorize';
public $Access_token_URL='https://api.twitter.com/oauth/access_token';
public $Callback_URL='http://www.dodofei.com';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';


public $Authorization_header;
public $signature_base_string;
public $oauth_signature;
public $signature_key;
public $curlgetinfo;
public $response_josn;
public $request_curldata;


public function newlogin(){

$this->oauth_para['oauth_callback']=$this->Callback_URL;
$this->oauth_para['oauth_consumer_key']=$this->oauth_consumer_key;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_signature']='';
$this->signature_key=rawurlencode($this->oauth_consumer_key_secret).'&';
}

public function oauth_set(){

//oauth_signature
ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp[count($temp)]=$key.'='.rawurlencode($val);
}
}

$bas_sting=implode('&',$temp);
$this->signature_base_string=rawurlencode($this->request_type).'&'.rawurlencode($this->Request_token_URL).'&'.rawurlencode($bas_sting);

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($this->signature_base_string);
}

// NG $this->oauth_para['oauth_signature']=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true)));
$this->oauth_para['oauth_signature']=base64_encode(hash_hmac('sha1',$this->signature_base_string,$this->signature_key,true));
$this->oauth_signature=$this->oauth_para['oauth_signature'];

if($this->debug=='on'){
echo '<pre>';
echo __LINE__;
print_r($temp);
}


//Authorization: OAuth
ksort($this->oauth_para);
$temp1=array();
$temp2=array();

foreach ($this->oauth_para as $key=>$val){
if(!empty($val)){
$temp1[count($temp1)]=$key.'="'.urlencode($val).'"';
//if ($key!='oauth_callback'){
$temp2[count($temp2)]=$key.'='.urlencode(rawurldecode($val));
//}
}
}

$this->Authorization_header='Authorization: OAuth '.implode(', ',$temp1);

if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp1);
}

//利用しない
$this->request_curldata=implode('&',$temp2);
if($this->debug=='on'){
echo '<pre><br>';
echo __LINE__;
print_r($temp2);
}

}



public function send_curl(){

//CURL 送信
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header,'Content-Length:','Expect:','Content-Tpye:'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));

if($this->request_type=='GET'){
//GET動かない
$geturl=$this->Request_token_URL.'?'.$this->request_curldata;
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
curl_setopt($ch, CURLOPT_URL, $geturl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
}

if($this->request_type=='POST'){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $this->Request_token_URL);
}

//curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
//echo $this->response_josn = json_decode(curl_exec($ch));
echo $this->response_josn = curl_exec($ch);
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
$this->curlgetinfo=curl_getinfo($ch);
//var_dump(curl_error($ch));
//var_dump($this->response_josn);
curl_close($ch);
}



public function debug(){

//Debug
echo '<br>/////////Debut///////////////////';
echo '<br><pre>';
echo 'request_datas<br>';
//echo print_r($this->request_datas);
echo '<br><br>';
echo 'Signature base string<br>';
echo $this->signature_base_string;
echo '<br><br>';
echo 'oauth_signature<br>';
echo $this->oauth_signature;
echo '<br><br>';
echo 'Authorization header<br>';
echo $this->Authorization_header;
echo '<br><br>';
echo 'Curl data<br>';
echo $this->request_curldata;
echo '<br><br>';
echo 'Curl_getinfo<br>';
echo print_r($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo print_r($this->response_josn);
echo '<br><br>';
echo '/////////Debut///////////////////';

}


}





?>

2013年9月16日月曜日

直接アクセス時にメッセージ

public $Request_token_URL='https://api.twitter.com/oauth/request_token';
public $Authorize_URL='https://api.twitter.com/oauth/authorize';
public $Access_token_URL='https://api.twitter.com/oauth/access_token';


直接アクセス時にメッセージ

https://api.twitter.com/oauth/authorize

エラーが発生しました
このページに対するリクエスト・トークンがありません。アプリケーションがTwitterアカウントを使用するかどうかを確認するために必要な特殊キーです。 送信したサイトまたはアプリケーションに戻って再度お試しください。何らかの手違いがあったかもしれません。




https://api.twitter.com/oauth/request_token

Failed to validate oauth signature and token






https://api.twitter.com/oauth/access_token

Invalid request token

2013年9月14日土曜日

REST API v1.1 動作確認 の続き

修正しました。
GET が動くようになった。

<?php
//date_default_timezone_set('Asia/Tokyo');
//echo '<br>--------timezone------<br>';
//echo date_default_timezone_get();
//echo '<br>--------timezone------<br>';
?>

<?php
//$paras['status']=rawurlencode('GET  動くようになった');
//$paras['since_id']=rawurlencode('14927799');
//$paras['count']=rawurlencode('50');
//$paras['since_id']=rawurlencode('259320959964680190');
//$paras['max_id']=rawurlencode('259320959964680500');
//$paras['q']=rawurlencode("アイドル");
//$paras['q']=rawurlencode('in China');


//$url='https://api.twitter.com/1.1/friends/ids.json';
$url='https://api.twitter.com/1.1/friendships/lookup.json';

$x=new twoauth('GET',$url,$paras);
$x->oauth_signature();
$x->send_curl();
$x->debug();

?>

<?php

class twoauth {
public $oauth_para=array();
public $oauth_consumer_key= '****************************';
public $oauth_consumer_key_secret= '*****************************';
public $oauth_token= '**************************************';
public $oauth_token_secret= '***********************************';
public $oauth_callback= 'http://www.xxx.com';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';
public $useragent = 'TwitterOAuth v0.2.0-beta2';

public $request_datas=array();
public $request_curldata;
public $request_type;
public $request_url;
public $signature_base_string;
public $signature_key;
public $oauth_signature;
public $Authorization_header;
public $curlgetinfo;
public $response_josn;


public function twoauth ($retype='POST',$url,$datas=null){

$this->oauth_para['oauth_consumer_key']=$this->oauth_consumer_key;
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_token']=$this->oauth_token;
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_signature'];
$this->signature_key=rawurlencode($this->oauth_consumer_key_secret).'&'.rawurlencode($this->oauth_token_secret);
$this->request_type=$retype;
$this->request_datas=$datas;
$this->request_url=$url;

}



public function oauth_signature(){

$bas_para=array();
if(!empty($this->request_datas) and $this->request_datas!=null){
$bas_para=$this->oauth_para + $this->request_datas;
}else{
$bas_para=$this->oauth_para;
}

ksort($bas_para);
$temp=array();
foreach ($bas_para as $key=>$val){
$temp[count($temp)]=$key.'='.$val;
}
$bas_sting=rawurlencode(implode('&',$temp));

$this->signature_base_string=$this->request_type.'&'.rawurlencode($this->request_url).'&'.$bas_sting;
$this->oauth_signature=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,sprintf($this->signature_key),true)));
$this->oauth_para['oauth_signature']=$this->oauth_signature;

ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
$temp[count($temp)]=$key.'="'.$val.'"';
}
$this->Authorization_header='Authorization: OAuth '.(implode(', ',$temp));

if (count($this->request_datas)!=0){
ksort($this->request_datas);
$temp=array();
if ($this->request_datas!=''){
foreach ($this->request_datas as $key=>$val){
$temp[count($temp)]=$key.'='.urlencode(rawurldecode($val));
}
}
$this->request_curldata=implode('&',$temp);
}else{
$this->request_curldata=abc;
}
}


public function send_curl(){

$ch=curl_init();
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ($this->request_curldata!=null){
if ($this->request_type=='POST'){
//POST
curl_setopt($ch, CURLOPT_URL, $this->request_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
}else if($this->request_type=='GET'){
//GET
if(!empty($this->request_datas)){
curl_setopt($ch, CURLOPT_URL, $this->request_url.'?'.rawurldecode(http_build_query($this->request_datas)));
}else{
curl_setopt($ch, CURLOPT_URL, $this->request_url);
}
}else{
//
exit("not support ($this->request_type) HTTP Methods");
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
$this->response_josn = json_decode(curl_exec($ch));
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
curl_close($ch);
}





public function debug(){

echo '<br><pre>';
echo 'request_datas<br>';
echo var_dump($this->request_datas);
echo '<br><br>';
echo 'Signature base string<br>';
echo $this->signature_base_string;
echo '<br><br>';
echo 'oauth_signature<br>';
echo $this->oauth_signature;
echo '<br><br>';
echo 'Authorization header<br>';
echo $this->Authorization_header;
echo '<br><br>';
echo 'Curl data<br>';
echo $this->request_curldata;
echo '<br><br>';
echo 'Curl_getinfo<br>';
echo var_dump($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo var_dump($this->response_josn);
echo '<br><br>';

}

}




?>

2013年9月13日金曜日

REST API v1.1 動作確認

REST API v1.1  Tweets POST statuses/update
一から作成したOauth 認証、API経由で投稿が成功しました。

注意店は status のエンコードです。
    Signature base string 作成時に、rawurlencode します。
    Authorization header 作成時に urlencode します。
ここに区別しないと、日本語の送信にエラーになります。

""{\"errors\":[{\"message\":\"Could not authenticate you\",\"code\":32}]}""






<?php
date_default_timezone_set('Asia/Tokyo');
echo '<br>--------timezone------<br>';
echo date_default_timezone_get();
echo '<br>--------timezone------<br>';
?>

<?php
$paras['status']=rawurlencode('テ スト 中 です。');
$url='https://api.twitter.com/1.1/statuses/update.json';
$x=new twoauth('POST',$url,$paras);
$x->oauth_signature();
$x->send_curl();
$x->debug();

?>

<?php

class twoauth {
public $oauth_para=array();
public $oauth_consumer_key= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_consumer_key_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_token= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_token_secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
public $oauth_callback= 'http://www.xxx.com';
public $oauth_signature_method= 'HMAC-SHA1';
public $oauth_version= '1.0';
public $useragent = 'TwitterOAuth v0.2.0-beta2';

public $request_datas=array();
public $request_curldata;
public $request_type;
public $request_url;
public $signature_base_string;
public $signature_key;
public $oauth_signature;
public $Authorization_header;
public $curlgetinfo;
public $response_josn;


public function twoauth ($retype='POST',$url,$datas=null){
$this->oauth_para['oauth_consumer_key']=$this->oauth_consumer_key;
$this->oauth_para['oauth_signature_method']=$this->oauth_signature_method;
$this->oauth_para['oauth_token']=$this->oauth_token;
$this->oauth_para['oauth_version']=$this->oauth_version;
$this->oauth_para['oauth_nonce']=md5(microtime().mt_rand());
$this->oauth_para['oauth_timestamp']=time();
$this->oauth_para['oauth_signature'];
$this->signature_key=rawurlencode($this->oauth_consumer_key_secret).'&'.rawurlencode($this->oauth_token_secret);
$this->request_type=$retype;
$this->request_url=$url;
$this->request_datas=$datas;
}



public function oauth_signature(){
$bas_para=array();
if(!empty($this->request_datas) and $this->request_datas!=null){
$bas_para=$this->oauth_para + $this->request_datas;
}else{
$bas_para=$this->oauth_para;
}

ksort($bas_para);
$temp=array();
foreach ($bas_para as $key=>$val){
$temp[count($temp)]=$key.'='.$val;
}
$bas_sting=rawurlencode(implode('&',$temp));

$this->signature_base_string=$this->request_type.'&'.rawurlencode($this->request_url).'&'.$bas_sting;
$this->oauth_signature=rawurlencode(base64_encode(hash_hmac('sha1',$this->signature_base_string,sprintf($this->signature_key),true)));
$this->oauth_para['oauth_signature']=$this->oauth_signature;

ksort($this->oauth_para);
$temp=array();
foreach ($this->oauth_para as $key=>$val){
$temp[count($temp)]=$key.'="'.$val.'"';
}
$this->Authorization_header='Authorization: OAuth '.(implode(', ',$temp));

if (count($this->request_datas)!=0){
ksort($this->request_datas);
$temp=array();
if ($this->request_datas!=''){
foreach ($this->request_datas as $key=>$val){
$temp[count($temp)]=$key.'='.urlencode(rawurldecode($val));
}
}
$this->request_curldata=implode('&',$temp);
}else{
$this->request_curldata=abc;
}
}


public function send_curl(){
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $this->request_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ($this->request_curldata!=null){
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_curldata);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->Authorization_header));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
$this->response_josn = json_decode(curl_exec($ch));
$this->curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
curl_close($ch);
}





public function debug(){

echo '<br><pre>';
echo 'request_datas<br>';
echo var_dump($this->request_datas);
echo '<br><br>';
echo 'Signature base string<br>';
echo $this->signature_base_string;
echo '<br><br>';
echo 'oauth_signature<br>';
echo $this->oauth_signature;
echo '<br><br>';
echo 'Authorization header<br>';
echo $this->Authorization_header;
echo '<br><br>';
echo 'Curl data<br>';
echo $this->request_curldata;
echo '<br><br>';
echo 'Curl_getinfo<br>';
echo var_dump($this->curlgetinfo);
echo '<br><br>';
echo 'JOSN data<br>';
echo var_dump($this->response_josn);
echo '<br><br>';

}

}




?>

2013年9月8日日曜日

Example request (Authorization header has been wrapped):

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
                     NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

oauth_signature

oauth_signature

$signature_key=$oauth_consumer_key_secret.'&'.$oauth_token_secret;

echo urlencode(base64_encode(hash_hmac('sha1', $sin, $signature_key,true)));