Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 3185  / 2 Years ago, sat, october 22, 2022, 7:50:33

I have searched the whole internet trying to find a simple example that could point me in the right direction, but no luck, so here comes my questions:



I want to log into Ubuntu One and sync (or almost read) files from my web page, all done with PHP. The needs to reach files are all described in that page: https://one.ubuntu.com/developer/account_admin/issue_tokens/cloud/



I am able to complete the first request with:



$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);


Ok, curlPetition only makes basic curl petitions. Note that you need a valid user:pass ubuntu one account. I get the response correctly in json with "consumer_secret" , "token" , "consumer_key" , "name" , "token_secret". Even the entry appear listed in the ubuntu one's granted apps.



I hace installed the most new OAuth PCL php extensión and its working good. but when I try to:



    $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());


I get moved to the "OpenID transaction in progress" page where you pass when doing a manual web login. Im definitely doing something wrong. I tried to get the second step from https://one.ubuntu.com/developer/account_admin/issue_tokens/cloud/ with $oauth->fetch, $oauth->getAccessToken and $oauth->getRequestToken, same response on all with 403 error :S



I was trying to figure how the payload works but the main examples are writed with python, using "import ubuntuone.couch.auth as auth" that makes the token stuff almost automatic.



I'll love to get some hints. Thanks


More From » ubuntu-one

 Answers
7

I believe the problem was that step 2 of the "create a new token" workflow, defined on https://one.ubuntu.com/developer/account_admin/issue_tokens/cloud/, was failing with a 503 for you because the service was down at a couple of points this weekend. You'll need to trap this situation and deal with it (a 503 indicates that you should retry the request later, as per standard HTTP).



I've tested the below PHP (beware: I am not a PHP hacker, so it might not be the most idiomatic code) and it works fine for me. It goes through three steps:




  1. Create a new token in Ubuntu SSO (login.ubuntu.com) (API docs)

  2. Tell Ubuntu One about that new token (API docs)

  3. Use that new token to sign a request to the Ubuntu One files API (API docs)



You'll see the individual parts commented below. Remember that this requests and gets a brand new token; once you have the token (after step 2), save it somewhere; don't request a new one every time.



<?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 = '[email protected]';
$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());
?>

[#44138] Sunday, October 23, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gavgenerati

Total Points: 120
Total Questions: 126
Total Answers: 119

Location: Marshall Islands
Member since Wed, Feb 9, 2022
2 Years ago
gavgenerati questions
Wed, Jun 22, 22, 17:24, 2 Years ago
Fri, Jul 30, 21, 04:32, 3 Years ago
Fri, Jan 14, 22, 14:20, 2 Years ago
Tue, Jul 27, 21, 19:05, 3 Years ago
;