从PHP进行Ubuntu One Oauth登录


8

我在整个互联网上进行搜索,试图找到一个简单的示例,可以为我指明正确的方向,但是没有运气,所以这里出现了我的问题:

我想登录Ubuntu One并从我的网页同步(或几乎读取)文件,所有这些工作都是通过PHP完成的。该页面中描述了到达文件的所有需求:https//one.ubuntu.com/developer/account_admin/issue_tokens/cloud/

我可以通过以下方式完成第一个请求:

$url = 'https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20try1';
$data = curlPetition(array('URL'=>$url,'USERPWD'=>'user:pass'));
$ar = fopen('uOne','w');fwrite($ar,$data['responseBody']);fclose($ar);
$tokenA = json_decode($data['responseBody'],1);

好的,curlPetition仅进行基本的卷曲请求。请注意,您需要一个有效的用户:通过ubuntu一个帐户。我可以使用“ consumer_secret”,“ token”,“ consumer_key”,“ name”,“ token_secret”在json中正确获得响应。甚至该条目也会出现在ubuntu授予的应用程序中。

我安装了最新的OAuth PCL phpextensión,并且工作正常。但是当我尝试:

    $api_url = 'https://one.ubuntu.com/api/file_storage/v1/';
    $conskey = $tokenA['consumer_key'];
    $conssec = $tokenA['consumer_secret'];
    $token = $tokenA['token'];
    $secret = $tokenA['token_secret'];
    $oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
    $oauth->enableDebug();
    $oauth->enableSSLChecks();
    $oauth->setToken($token,$secret);
    $oauth->fetch($api_url.'~/Ubuntu%20One/');
    print_r($oauth->getLastResponse());

我将转到“正在进行的OpenID交易”页面,您在进行手动Web登录时会通过该页面。我肯定做错了。我试图通过$ oauth-> fetch,$ oauth-> getAccessToken和$ oauth-> getRequestToken 从https://one.ubuntu.com/developer/account_admin/issue_tokens/cloud/获得第二步,所有响应都相同403错误:S

我试图弄清楚有效负载是如何工作的,但是主要示例是使用“ import ubuntuone.couch.auth as auth”使用python编写的,这使得令牌的工作几乎自动化了。

我很乐意得到一些提示。谢谢


1
在获得令牌(步骤1)之后,我尝试使用PHP的所有示例,然后用令牌本身进行签名(步骤2),这很好,在步骤3中,当我应该获取文件列表时,服务器返回:禁止(403),CSRF验证失败了 请求中止。在Internet上搜索时,我发现这是一个错误报告(https://bugs.launchpad.net/ubuntuone-servers/+bug/686697),但它被标记为“已修复”,但是对我来说仍然无法正常工作。

Answers:


11

我认为问题在于在https://one.ubuntu.com/developer/account_admin/issue_tokens/cloud/上定义的“创建新令牌”工作流程的第2步为您失败,并显示503,因为该服务这个周末下降了几个点。您需要捕获这种情况并加以处理(503表示您应稍后根据标准HTTP重试该请求)。

我已经测试了以下PHP(请注意:我不是PHP黑客,所以它可能不是最惯用的代码),它对我来说很好用。它经历了三个步骤:

  1. 在Ubuntu SSO(login.ubuntu.com)中创建新令牌(API文档
  2. 向Ubuntu One告知该新令牌(API文档
  3. 使用该新令牌来签署对Ubuntu One文件API的请求(API docs

您将在下面看到注释的各个部分。请记住,这要求并获得一个全新的令牌;获得令牌后(在第2步之后),将其保存在某处;不要每次都要求一个新的。

<?php
function curlPetition($arr){
    $curl = curl_init($arr['URL']);
    if($arr['USERPWD']){curl_setopt($curl, CURLOPT_USERPWD, $arr['USERPWD']);}  
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    $out = curl_exec($curl);
    curl_close($curl);
    $data['responseBody'] = $out;
    return $data;
}

/* Define username and password details */
$email_address = 'stuart.langridge@canonical.com';
$password = 'MY PASSWORD';

/* Step 1: Get a new OAuth token from Ubuntu Single-Sign-On */
$url = 'https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20try1';
$data = curlPetition(array('URL'=>$url,'USERPWD'=> $email_address.':'.$password));
$tokenA = json_decode($data['responseBody'],1);

/* Set up that new token for use in OAuth requests */
$conskey = $tokenA['consumer_key'];
$conssec = $tokenA['consumer_secret'];
$token = $tokenA['token'];
$secret = $tokenA['token_secret'];
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
$oauth->enableSSLChecks();
$oauth->setToken($token,$secret);

/* Step 2: tell Ubuntu One about the new token (signed with the token itself) */
$tell_u1_about_token_url = 'https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/' . $email_address;
$oauth->fetch($tell_u1_about_token_url);
print_r($oauth->getLastResponse());

/* Step 3: use the token to make a request to the Files API */
$api_url = 'https://one.ubuntu.com/api/file_storage/v1/';
$oauth->fetch($api_url.'~/Ubuntu%20One/');
print_r($oauth->getLastResponse());
?>

顺便说一句,如果您决定为Ubuntu One Files API编写PHP包装器库,请告诉我有关它,并从文档中链接至该库。完成后,我也希望能看到您的网站!
sil

我也对此感兴趣。当您说“一旦拥有令牌(在第2步之后),将其保存在某处”时,令牌与使用api的应用程序或用户相关?我认为对用户来说,但它始终是相同的,还是在注销后或会话结束时删除了它?
Matteo Pagliazzi 2011年

令牌与两者都有某种关联。该令牌是特定于用户的,但它也是一个单独的令牌,已发行给您的应用程序以供该用户使用。它永远有效,但是用户可以删除该令牌(并因此撤销您的应用程序对Ubuntu One的访问权限)。令牌不用于应用程序-不是API密钥。您的应用程序不应将令牌硬编码到其源代码中!
sil

2

来自第一步课程的代码与ubuntuOne交谈

<?php
class ubuntuOne{
    var $curl = array('cookieSrc'=>'cookie.txt','enableCookies'=>false);
    var $auth = array('consumer_key'=>false,'consumer_secret'=>false,'token'=>false,'token_secret'=>false);
    var $oauth = false;
    function ubuntuOne(){

    }
    function u1_getRoot(){
        if($this->oauth === false){return false;}
        $url = 'https://one.ubuntu.com/api/file_storage/v1';
        $this->oauth->fetch($url);
        print_r($this->oauth->getLastResponse());
    }
    function u1_listFolder($path){
        if($this->oauth === false){return false;}
        //FIXME: parse $path
        $url = 'https://one.ubuntu.com/api/file_storage/v1';
        //FIXME: $path debe terminar en '/'
        $url .= str_replace(' ','%20',$path);

        $this->oauth->fetch($url);
        $lr = $this->oauth->getLastResponse();
        if($lr === '{"error": "not found"}'){return false;}
        print_r($this->oauth->getLastResponse());
    }
    function u1_createFolder($name,$path = ''){
        //FIXME: folder exists?
        $url = 'https://one.ubuntu.com/api/file_storage/v1';
        //FIXME: $path debe terminar en '/'
        $url .= str_replace(' ','%20',$path);
        //FIXME: $name no puede contener '/'
        $url .= str_replace(' ','%20',$name);

        $this->oauth->fetch($url,'{"kind":"directory"}',OAUTH_HTTP_METHOD_PUT);
        print_r($this->oauth->getLastResponse());
    }
    function u1_file_exists($path){
        //FIXME: cache?
        $url = 'https://one.ubuntu.com/api/file_storage/v1';
        $url .= str_replace(' ','%20',$path);

        try{$this->oauth->fetch($url);}
        catch(OAuthException $E){if($E->lastResponse === '{"error": "not found"}'){return false;}}
        $i = $this->oauth->getLastResponseInfo();
        if($i['http_code'] === 200){}
        print_r($this->oauth->getLastResponseInfo());
    }
    function requestAuthentification($user,$pass,$name){
        $url = 'https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20'.rawurlencode($name);
        $data = curlPetition(array('URL'=>$url,'USERPWD'=>$user.':'.$pass));
        //FIXME: check the response header -> 200
        $this->auth = json_decode($data['responseBody'],1);
    }
    function registerToken($user){
        $url = 'https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/'.$user;
        $this->oauth->fetch($url);
        $r = $this->oauth->getLastResponse();
        if(substr($r,02) !== 'ok'){
            //FIXME: poner error
        }
        print_r($this->oauth->getLastResponse());
    }
    function saveAuth($fileName){$ar = fopen($fileName,'w');fwrite($ar,json_encode($this->auth));fclose($ar);return true;}
    function loadAuth($fileName){
        if(!file_exists($fileName)){return false;}
        $this->auth = json_decode(file_get_contents($fileName),1);
        if($this->auth === NULL){return false;}
        $this->oauth = new OAuth($this->auth['consumer_key'],$this->auth['consumer_secret'],OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
        $this->oauth->enableDebug();
        $this->oauth->enableSSLChecks();
        $this->oauth->setToken($this->auth['token'],$this->auth['token_secret']);
        return true;
    }
    function curlPetition($arr,$data = array()){
        $curl = curl_init($arr['URL']);
        if(!isset($data['URLTRACK'])){$data['URLTRACK'] = array();}
        $data['URLTRACK'][] = $arr['URL'];

        if(isset($this->curl['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$this->curl['userAgent']);}
        if(count($data['URLTRACK']) > 1){curl_setopt($curl,CURLOPT_REFERER,$data['URLTRACK'][count($data['URLTRACK'])-2]);}
        if(isset($arr['USERPWD'])){curl_setopt($curl,CURLOPT_USERPWD,$arr['USERPWD']);}
        if(isset($arr['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$arr['userAgent']);}
        if(isset($arr['POST'])){curl_setopt($curl,CURLOPT_POST,true);curl_setopt($curl,CURLOPT_POSTFIELDS,$arr['POST']);}
        if(isset($arr['referer'])){curl_setopt($curl,CURLOPT_REFERER,$arr['referer']);}
        if(isset($arr['timeout'])){curl_setopt($curl,CURLOPT_TIMEOUT,$arr['timeout']);}
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HEADER,true);

        if($this->curl['enableCookies'] !== false ){$cookieSrc = $this->curl['cookieSrc'];curl_setopt($curl,CURLOPT_COOKIEFILE,$cookieSrc);curl_setopt($curl,CURLOPT_COOKIEJAR,$cookieSrc);}
        if(isset($arr['header'])){curl_setopt($curl,CURLOPT_HTTPHEADER,$arr['header']);}
        curl_setopt($curl,CURLOPT_TIMEOUT,25);

        $viewcode = curl_exec($curl);
        $curlInfo = curl_getinfo($curl);
        curl_close($curl);
        if(empty($viewcode)){return false;}
        $data['responseHeader'] = substr($viewcode,0,$curlInfo['header_size']);
        $data['responseBody'] = substr($viewcode,$curlInfo['header_size']);
        //$data['viewcode'] = $viewcode;

        if(isset($arr['FOLLOWLOCATION']) && preg_match('/HTTP\/1\.1 30[12]{1}/',$data['responseHeader'])){
            preg_match('/Location: (.*)/',$data['responseHeader'],$p);
            $nurl = trim($p[1]);
            if($nurl[0]=='/'){list($arr['URL'],) = explode('/',str_replace('http://','',$arr['URL']));$nurl = 'http://'.$arr['URL'].$nurl;}
            $arr['URL'] = $nurl;
            unset($arr['POST']);
            return curlPetition($arr,$data);
        }

        return $data;
    }
}
?>

一些示例调用(很麻烦的代码和注释的代码,也许有一天有文档说明):

echo time()."\n";
$ub = new ubuntuOne;
/* The first time you made the commented calls, then you save the authorization 
 * to a file. Once you have it on a file, you load it every time from there */
//$ub->requestAuthentification('sombra2eternity@gmail.com','*****','st');
//$ub->registerToken('sombra2eternity@gmail.com');
//print_r($ub->auth);
//$ub->saveAuth($GLOBALS['userPath'].'db/uOne.protected');
$ub->loadAuth($GLOBALS['userPath'].'db/uOne.protected');
//$ub->registerToken('sombra2eternity@gmail.com');
$ub->u1_getRoot();
//$ub->u1_file_exists('/~/Ubuntu One/non_exists/');
echo "\n";
$ub->u1_listFolder('/~/Ubuntu One/');
echo "\n";
$ub->u1_createFolder('new folder','/~/Ubuntu One/');

祝您好运,希望对您有所帮助


1

更新版本,添加了一些功能,发现了一些错误

    class ubuntuOne{
        var $curl = array('cookieSrc'=>'cookie.txt','enableCookies'=>false);
        var $auth = array('consumer_key'=>false,'consumer_secret'=>false,'token'=>false,'token_secret'=>false);
        var $oneInfo = false;
        var $oauth = false;
        var $E = array('errorCode'=>0,'errorDescription'=>'');
        var $fs = array();
        function ubuntuOne(){
            $this->fs['/'] = $this->helper_nodeSkeleton(array('name'=>'/','kind'=>'directory','resource_path'=>'/'));
        }
        function helper_nodeSkeleton($a = array()){return array_merge(array('name'=>false,'kind'=>false,'when_created'=>false,'generation'=>false,'has_children'=>false,'content_path'=>false,'generation_created'=>false,'parent_path'=>false,'resource_path'=>false,'when_changed'=>false,'key'=>false,'path'=>false,'volume_path'=>false,'size'=>0,'children'=>array()),$a);}
        function helper_storePath($path,$node = false){
            $path = explode('/',$path);
            $curPath = &$this->fs['/'];
            $resPath = '';
            foreach($path as $p){if($p === ''){continue;}$resPath .= '/'.$p;if(!isset($curPath['children'][$p])){$curPath['children'][$p] = $this->helper_nodeSkeleton(array('name'=>$p,'kind'=>'directory','resource_path'=>$resPath));}$curPath = &$curPath['children'][$p];}
            if($node !== false){$curPath = array_merge($curPath,$node);if($curPath['kind'] == 'file'){unset($curPath['children']);}}
        }
        function helper_storeNode($node){
            if(!isset($node['name'])){$r = preg_match('/\/([^\/]+)$/',$node['resource_path'],$name);if($r === 0){$this->E = array('errorCode'=>1,'errorDescription'=>'NAME_NOT_PARSED');return null;}$node['name'] = $name[1];}
            $this->helper_storePath($node['resource_path'],$node);
            $this->E = array('errorCode'=>0,'errorDescription'=>'');
        }
        function u1_getRoot(){
            if($this->oauth === false){return false;}
            $url = 'https://one.ubuntu.com/api/file_storage/v1';
            try{$this->oauth->fetch($url);}
            catch(OAuthException $E){print_r($E);return false;}
            $lr = json_decode($this->oauth->getLastResponse(),1);
            $this->helper_storePath($lr['root_node_path']);
            foreach($lr['user_node_paths'] as $np){$this->helper_storePath($np);}
            $this->oneInfo = $lr;
            return $lr;
        }
        function u1_getVolumeTree(){
            if($this->oneInfo === false){$r = $this->u1_getRoot();if($r === null){return $r;}}
            $base = $this->fs['/']['children']['~']['children'];
            foreach($base as $k=>$node){$this->u1_helper_getVolumeTree($node);}
            return $this->fs;
        }
        function u1_helper_getVolumeTree($node,$i = 0){
            if($node['kind'] == 'file'){return;}
            $r = $this->u1_folder_list($node['resource_path']);
            foreach($r['children'] as $child){$this->u1_helper_getVolumeTree($child,$i);}
        }
        function u1_folder_list($path){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            if(substr($path,-1) != '/'){$path .= '/';}
            $url = 'https://one.ubuntu.com/api/file_storage/v1'.$this->helper_encodeURL($path).'?include_children=true';

            try{$this->oauth->fetch($url);}
            catch(OAuthException $E){echo $path;print_r($E);return null;}
            $lr = $this->oauth->getLastResponse();
            if($lr === '{"error": "not found"}'){return null;}
            $lr = json_decode($lr,1);

            /* Store the base node */
            $node = $lr;unset($node['children']);
            $this->helper_storeNode($node);
            foreach($lr['children'] as $child){$this->helper_storeNode($child);}
            return $lr;
        }
        function u1_folder_create($name,$path = '/~/Ubuntu One/'){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            if(substr($path,-1) != '/'){$path .= '/';}
            $name = preg_replace(array('/[\.]$/','/[\/]*/'),'',$name);

            //FIXME: folder exists?
            $url = 'https://one.ubuntu.com/api/file_storage/v1'.$this->helper_encodeURL($path).$this->helper_encodeURL($name);

            $this->oauth->fetch($url,'{"kind":"directory"}',OAUTH_HTTP_METHOD_PUT);
            $node = json_decode($this->oauth->getLastResponse(),1);
            $this->helper_storeNode($node);
            return $node;
        }
        function u1_file_create($path,$blob){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            //if(substr($path,-1) != '/'){$path .= '/';}
            $url = 'https://files.one.ubuntu.com/content'.$this->helper_encodeURL($path);
            //FIXME: u1_file_exists

            $this->oauth->fetch($url,$blob,OAUTH_HTTP_METHOD_PUT,array('Content-Type'=>'application/json'));
            //$i = $this->oauth->getLastResponseInfo();
            //print_r($i);
            $node = json_decode($this->oauth->getLastResponse(),1);
            $this->helper_storeNode($node);
            return $node;
        }
        function u1_file_exists($path,$nocache = false){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            //FIXME: cache?
            $url = 'https://one.ubuntu.com/api/file_storage/v1'.$this->helper_encodeURL($path);

            try{$this->oauth->fetch($url);}
            catch(OAuthException $E){if($E->lastResponse === '{"error": "not found"}'){return false;}}
            $i = $this->oauth->getLastResponseInfo();
            if($i['http_code'] === 200){}
            //FIXME: respuesta adecuada
            print_r($this->oauth->getLastResponseInfo());
        }
        function u1_file_get($contentPath,$destinyPath = false){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            if(substr($contentPath,0,9) != '/content/'){$this->E = array('errorCode'=>1,'errorDescription'=>'NO_CONTENT_PATH');return null;}
            $url = 'https://files.one.ubuntu.com'.$this->helper_encodeURL($contentPath);

            /* I hope nobody ask me about the following concat, never gonna give you up!! */
            $time = time();
            $data = array('oauth_consumer_key'=>$this->auth['consumer_key'],'oauth_nonce'=>$time*rand(0,200),'oauth_signature_method'=>'HMAC-SHA1','oauth_timestamp'=>$time,'oauth_token'=>$this->auth['token'],'oauth_version'=>'1.0');
            $b = '';foreach($data as $k=>$v){$b .= '&'.$k.'='.$v;}
            $b = 'GET&'.rawurlencode($url).'&'.rawurlencode(substr($b,1));

            $key = $this->auth['consumer_secret'].'&'.$this->auth['token_secret'];
            $signature = $this->helper_oauth_hmacsha1($key,$b);

            $data['oauth_signature'] = $signature;
            $a = $url.'?';foreach($data as $k=>$v){$a .= $k.'='.rawurlencode($v).'&';}

            $h = fopen($a,'r');
            if(!$h){
                //FIXME: poner error
                return null;
            }

            //FIXME: is_writable
            //FIXME: file_exists
            $fileName = basename($contentPath);
            $ar = fopen($destinyPath.$fileName,'w');

            //FIXME: comprobar los primeros bits del buffer para asegurarse de que no está fallando
            $buffer = '';while(!feof($h)){$buffer = fgets($h,8192);fwrite($ar,$buffer);}fclose($h);

            fclose($ar);
            $filehash = sha1_file($destinyPath.$fileName);
            //echo "\n".$filehash."\n";

            return array('fileName'=>$fileName,'filePath'=>$destinyPath,'fileHash'=>$filehash);
        }
        function u1_file_unlink($path){
            if($this->oauth === false){$this->E = array('errorCode'=>99,'errorDescription'=>'NO_OAUTH_DATA');return null;}
            $url = 'https://one.ubuntu.com/api/file_storage/v1'.$this->helper_encodeURL($path);
            //FIXME: u1_file_exists

            try{$this->oauth->fetch($url,$blob,OAUTH_HTTP_METHOD_DELETE,array('Content-Type'=>'application/json'));}
            catch(OAuthException $E){print_r($E);$this->E = array('errorCode'=>1,'errorDescription'=>'FILE_NOT_EXISTS');return null;}
            $i = $this->oauth->getLastResponseInfo();
    //FIXME: eliminar el fichero de la caché
            //print_r($i);
        }
        function requestAuthentification($user,$pass,$name){
            $url = 'https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20'.rawurlencode($name);
            $data = $this->curlPetition(array('URL'=>$url,'USERPWD'=>$user.':'.$pass));
            //FIXME: check the response header -> 200
            $this->auth = json_decode($data['responseBody'],1);
            if($this->auth === NULL){return false;}
            $this->oauth = new OAuth($this->auth['consumer_key'],$this->auth['consumer_secret'],OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
            $this->oauth->enableDebug();
            $this->oauth->enableSSLChecks();
            $this->oauth->setToken($this->auth['token'],$this->auth['token_secret']);
            return true;
        }
        function registerToken($user){
    //FIXME: check $this->oauth == false
            $url = 'https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/'.$user;
            $this->oauth->fetch($url);
            $r = $this->oauth->getLastResponse();
            if(substr($r,02) !== 'ok'){
                //FIXME: poner error
            }
            //print_r($this->oauth->getLastResponse());
        }
        function saveAuth($fileName){$ar = fopen($fileName,'w');fwrite($ar,json_encode($this->auth));fclose($ar);return true;}
        function loadAuth($fileName){
            if(!file_exists($fileName)){return false;}
            $this->auth = json_decode(file_get_contents($fileName),1);
            if($this->auth === NULL){return false;}
            return $this->helper_makeOauth();
        }
        function setAuth($auth){$this->auth = $auth;return $this->helper_makeOauth();}
        function helper_makeOauth(){
            $this->oauth = new OAuth($this->auth['consumer_key'],$this->auth['consumer_secret'],OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
            $this->oauth->enableDebug();
            $this->oauth->enableSSLChecks();
            $this->oauth->setToken($this->auth['token'],$this->auth['token_secret']);
            return true;
        }
        function helper_encodeURL($url){return str_replace('%2F','/',rawurlencode($url));}
        function helper_oauth_hmacsha1($key,$data){
            $blocksize=64;$hashfunc='sha1';
            if(strlen($key)>$blocksize){$key=pack('H*',$hashfunc($key));}
            $key=str_pad($key,$blocksize,chr(0x00));
            $ipad=str_repeat(chr(0x36),$blocksize);
            $opad=str_repeat(chr(0x5c),$blocksize);
            $hmac=pack('H*',$hashfunc(($key^$opad).pack('H*',$hashfunc(($key^$ipad).$data))));
            return base64_encode($hmac);
        }
        function curlPetition($arr,$data = array()){
            //FIXME: data puede ser una propiedad de la clase
            $curl = curl_init($arr['URL']);
            if(!isset($data['URLTRACK'])){$data['URLTRACK'] = array();}
            $data['URLTRACK'][] = $arr['URL'];

            if(isset($this->curl['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$this->curl['userAgent']);}
            if(count($data['URLTRACK']) > 1){curl_setopt($curl,CURLOPT_REFERER,$data['URLTRACK'][count($data['URLTRACK'])-2]);}
            if(isset($arr['USERPWD'])){curl_setopt($curl,CURLOPT_USERPWD,$arr['USERPWD']);}
            if(isset($arr['userAgent'])){curl_setopt($curl,CURLOPT_USERAGENT,$arr['userAgent']);}
            if(isset($arr['POST'])){curl_setopt($curl,CURLOPT_POST,true);curl_setopt($curl,CURLOPT_POSTFIELDS,$arr['POST']);}
            if(isset($arr['referer'])){curl_setopt($curl,CURLOPT_REFERER,$arr['referer']);}
            if(isset($arr['timeout'])){curl_setopt($curl,CURLOPT_TIMEOUT,$arr['timeout']);}
            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,2);
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($curl,CURLOPT_HEADER,true);

            if($this->curl['enableCookies'] !== false ){$cookieSrc = $this->curl['cookieSrc'];curl_setopt($curl,CURLOPT_COOKIEFILE,$cookieSrc);curl_setopt($curl,CURLOPT_COOKIEJAR,$cookieSrc);}
            if(isset($arr['header'])){curl_setopt($curl,CURLOPT_HTTPHEADER,$arr['header']);}
            curl_setopt($curl,CURLOPT_TIMEOUT,25);

            $viewcode = curl_exec($curl);
            $curlInfo = curl_getinfo($curl);
            curl_close($curl);
            if(empty($viewcode)){return false;}
            $data['responseHeader'] = substr($viewcode,0,$curlInfo['header_size']);
            $data['responseBody'] = substr($viewcode,$curlInfo['header_size']);
            //$data['viewcode'] = $viewcode;

            if(isset($arr['FOLLOWLOCATION']) && preg_match('/HTTP\/1\.1 30[12]{1}/',$data['responseHeader'])){
                preg_match('/Location: (.*)/',$data['responseHeader'],$p);
                $nurl = trim($p[1]);
                if($nurl[0]=='/'){list($arr['URL'],) = explode('/',str_replace('http://','',$arr['URL']));$nurl = 'http://'.$arr['URL'].$nurl;}
                $arr['URL'] = $nurl;
                unset($arr['POST']);
                return $this->curlPetition($arr,$data);
            }

            return $data;
        }
    }

一个简单的例子:$ ub = new ubuntuOne; $ r = $ ub-> requestAuthentification($ arr ['user'],$ arr ['pass'],$ arr ['name']); if($ r === false){返回json_encode(array('errorCode'=> 2,'errorDescription'=>'AUTH_ERROR','file'=> __ FILE __,'line'=> __ LINE__));} $ r = $ ub-> registerToken($ arr ['user']); if($ r === false){返回json_encode(array('errorCode'=> 3,'errorDescription'=>'TOKEN_ERROR','file'=> __ FILE __,'line'=> __ LINE__));} $ files = $ ub-> u1_getVolumeTree(); print_r($ files);
Marcos Fernandez Ramos

0

哇,Stuart Langridge,你就像我的传奇!

我想明天我将在您的示例中找到一些闲暇时间,看看我能得到什么。同时,我制定了一些基于Curl的函数来登录和掠夺UbuntuOne HTML页面。一旦稳定一下,我将尝试将其发布在这里。

是的,我将编写一个几乎完整的基于PHP的API,并且会发现Gimme会通知您,我现在有点超载:S

我很乐意向您展示我的工作,也许有一天我会克服恐惧,申请在Canonical工作,这对我来说就像一个梦想。就目前而言,我已经写了一篇简短的评论来向您展示我当前的项目,如果您愿意,我很乐意向您发送一份副本。我想以自由软件的形式发布它,但是我是需要掩盖网络项目并确保它在发布前是安全的人之一。

(抱歉,这不是任何形式的永久链接) http://marcos.colorvamp.com/mongo/lain-and-ubuntu-one/

而且,如果您检查基本页面……嗯,就像在我的土地上所说的那样。“在铁匠的房子里,有木刀” :-)

谢谢您的回复 :-)


这东西看起来很酷!我很高兴与您聊聊如何最有效地与Ubuntu One结合。然后,我可以告诉您如何在不对HTML页面进行屏幕抓取的情况下做您想做的事情,我们希望您不要这样做。:)
sil

收到您的任何提示我感到非常荣幸。我的项目有一些需要处理的需求(界面,核心,安全性,js应用程序,权限),因此我的进度有时会有些缓慢,通常是在我必须触摸某些区域时。我将在这里发布实现ubuntu One沟通第一步的基本课程,虽然还没有准备好,但是如果有任何疑问,可以将一些人指出正确的方向。我将在空闲时间更新它,如果可以的话,将其发布回这里。
Marcos Fernandez Ramos

无论如何,当我完成“发布”协议时,如果您有一点空闲时间,我想给您发送一份副本,并获得您的反馈。它充满了“小贴士”(测试用例,过多的台面开发阅读量:)),以避免作为示例的回归。也许UbuntuOne团队可以从代码中获得一些缓存想法(我对此表示怀疑,但仍然...)。谢谢你的时间!
Marcos Fernandez Ramos

一定要给我发送副本和详细信息,等等。如果您想赶上聊天的时间,或者说我们可以进行Skype或其他操作,我将以“水瓶座”的身分出现在IRC上:)
sil
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.