NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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's gravatar image

jcj7421
11112
accept rate: 0%

1

Please don't test against the main server!

(07 Nov '16, 19:34) Andy Allan

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

(07 Nov '16, 20:31) jcj7421

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×290
×22
×5

question asked: 07 Nov '16, 10:30

question was seen: 2,157 times

last updated: 07 Nov '16, 20:31

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum