Hello,
I have build my own OSM server, then I am testing how to modify data on real OSM web sites before doing it on my own server.
I followed the russian PHP example available at the end of this page : http://wiki.openstreetmap.org/wiki/OAuth/examples
So everything works until I try to write in OSM web site, neither in the development osm server (http://api06.dev.openstreetmap.org) nor in the production osm server (http://www.openstreetmap.org).
Each time I am doing a PUT request, it fails with a '401 Unauthorized' message.
Can somebody help me ?
Here are the PHP code (I write all token variables in include files rather thant using session variables or cookies) :
1) step1.php (requests a user's token, writes it in an include file, then call the authorization url) :
<?php
include 'parameters.php';
try {
$oauth = new OAuth($consumer_key, $consumer_secret,
OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
// requiring user's token
$future_token_user = $oauth->getRequestToken($req_url);
$future_user_token_name = $future_token_user['oauth_token'];
$future_user_token_secret = $future_token_user['oauth_token_secret'];
// writing user's token
$fp = fopen ($include_token_user, 'w');
fprintf ($fp, "<?php\n%s\n%s\n?>\n",
'$oauth_user_token = \''.$future_user_token_name.'\';',
'$oauth_user_token_secret = \''.$future_user_token_secret.'\';');
fclose ($fp);
echo "<a href=\"".$authurl."?oauth_token=".$future_user_token_name."\">To authorize</a>";
} catch(OAuthException $E) {
print_r($E);
}
?>
2) step2.php (executed by the authorization callback url ; starts reading user's token from include file, requests a session token, writes it in another include file, reads user's data ok, writes changeset ko) :
<?php
include 'parameters.php';
include 'reading.php';
include 'writing.php';
// reading user's token
include $include_token_user;
// some tests to be consistent
if(!isset($_GET['oauth_token'])) {
echo "No token!";
exit;
}
$token = $_GET['oauth_token'];
if ($token != $oauth_user_token) {
echo "Authentication error : current token (".$token
.") is not the same as during step1 (".$oauth_user_token
."). Please redo it";
exit;
}
try {
$oauth = new OAuth($consumer_key, $consumer_secret,
OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
// using user's token
$oauth->setToken($oauth_user_token, $oauth_user_token_secret);
// requiring session's token
$session_token = $oauth->getAccessToken($acc_url);
$session_token_name = strval($session_token['oauth_token']);
$session_token_secret = strval($session_token['oauth_token_secret']);
// using session's token
$oauth->setToken($session_token_name, $session_token_secret);
// writing session's token
$fp = fopen ($include_token_session, 'w');
fprintf ($fp, "<?php\n%s\n%s\n?>\n",
'$oauth_session_token = \''.$session_token_name.'\';',
'$oauth_session_secret = \''.$session_token_secret.'\';');
fclose ($fp);
reading ();
writing ();
} catch(OAuthException $E) {
print_r($E);
}
?>
3) step3.php, made just to demonstrate that we can start from the session's token, but it's the same behaviour as step2.php (reads session's token from include file, reads user's data ok, writes changeset ko) :
<?php
include 'parameters.php';
include 'reading.php';
include 'writing.php';
// reading sessions's token
include $include_token_session;
try {
$oauth = new OAuth($consumer_key, $consumer_secret,
OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
// using session's token
$oauth->setToken($oauth_session_token, $oauth_session_secret);
reading ();
writing ();
} catch(OAuthException $E) {
print_r($E);
}
?>
4) and all include files :
parameters.php :
<?php
$req_url = 'http://api06.dev.openstreetmap.org/oauth/request_token'; // OSM Request Token URL
$authurl = 'http://api06.dev.openstreetmap.org/oauth/authorize'; // OSM Authorize URL
$acc_url = 'http://api06.dev.openstreetmap.org/oauth/access_token'; // OSM Access Token URL
$api_url = 'http://api06.dev.openstreetmap.org/api/0.6/'; // OSM API URL
// application keys
$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// paths to include files
$include_token_user = '/var/www/html/russian/token_user.php';
$include_token_session = '/var/www/html/russian/token_session.php';
?>
reading.php :
<?php
function reading () {
global $oauth, $api_url;
/// Reading user's data through /api/0.6/user/details
$oauth->fetch($api_url."user/details");
$user_details = $oauth->getLastResponse();
// the output response
echo str_replace("\n", "<br/>",
htmlentities($oauth->getLastResponse()))."<br/>";
// parse response, obtain the Osm user's name and his id
$xml = simplexml_load_string($user_details);
echo "<br/>";
// Reading user's preferences
$oauth->fetch($api_url."user/preferences");
echo str_replace("\n", "<br/>",
htmlentities($oauth->getLastResponse()))."<br/>";
}
writing.php :
<?php
function writing () {
global $oauth, $api_url;
// writing changeset (with put)
$changeset = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
."<osm version=\"0.6\" generator=\"testrussian\">"
." <changeset>"
." <tag k=\"created_by\" v=\"My OSM Service\"/>"
." <tag k=\"comment\" v=\"Test changeset\"/>"
." </changeset>"
." </osm>";
$oauth->fetch($api_url."changeset/create", $changeset,
OAUTH_HTTP_METHOD_PUT);
$idchangeset = str_replace("\n", "<br/>",
htmlentities($oauth->getLastResponse()));
echo $idchangeset."<br/>";
// Well, close the changer.
$oauth->fetch($api_url."changeset/".$idchangeset."/close", "",
OAUTH_HTTP_METHOD_PUT);
echo str_replace("\n", "<br/>",
htmlentities($oauth->getLastResponse()))."<br/>";
}
asked
07 Nov '16, 10:30
jcj7421
11●1●1●2
accept rate:
0%
Please don't test against the main server!
Hello Andy. Of course, I make my tests almost all the time with the test server (api06.dev.openstreetmap.org), but you know, when you use an offical exemple, you follow the documentation but your program fails, and you don't understand why, you will make a lot of tests, including one with the main server, just in case. Regards, JC