Permalink
Browse files

add lib for commonshttp (needed for oauth) & some progress in making …

…oauth work
  • Loading branch information...
1 parent 20bd9c6 commit a36d1df079614c9e503d0e1aaab2844b31b10231 Stefan committed Feb 4, 2012
View
@@ -16,5 +16,6 @@
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/signpost-core-1.2.1.1.jar"/>
+ <classpathentry kind="lib" path="libs/signpost-commonshttp4-1.2.1.1.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Binary file not shown.
View
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" >
+
+ <Button
+ android:id="@+id/button_set_osm"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/set_osm" />
+
+ <TextView
+ android:id="@+id/textView_osm_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_secret_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_secret_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_secret_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_access_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_access_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_access_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+</LinearLayout>
View
@@ -29,5 +29,9 @@
<string name="emptyString"></string>
<string name="transmitChangesString">Transmit Changes to OpenStreetMap</string>
<string name="enableEditmode">Toggle Editmode</string>
+ <string name="set_osm">Set OSM-Account</string>
+ <string name="osm_token">Your Token:</string>
+ <string name="osm_secret_token">Your Secret Token:</string>
+ <string name="osm_access_token">Your Access Token:</string>
</resources>
View
@@ -0,0 +1,200 @@
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Environment;
+import android.preference.PreferenceManager;
+import oauth.signpost.OAuthConsumer;
+import oauth.signpost.OAuthProvider;
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
+import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import java.io.File;
+
+public class OSMHelper implements IActionListener
+{
+
+ IActionListener callback;
+ Context ctx;
+
+ public OSMHelper(Context ctx, IActionListener callback)
+ {
+
+ this.ctx = ctx;
+ this.callback = callback;
+ }
+
+ public static OAuthProvider GetOSMAuthProvider(Context ctx)
+ {
+ return new CommonsHttpOAuthProvider(
+ ctx.getString(R.string.osm_requesttoken_url),
+ ctx.getString(R.string.osm_accesstoken_url),
+ ctx.getString(R.string.osm_authorize_url));
+
+ }
+
+ public static boolean IsOsmAuthorized(Context ctx)
+ {
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(ctx);
+ String oAuthAccessToken = prefs.getString("osm_accesstoken", "");
+
+ return (oAuthAccessToken != null && oAuthAccessToken.length() > 0);
+ }
+
+ public static Intent GetOsmSettingsIntent(Context ctx)
+ {
+ Intent intentOsm;
+
+ if (!IsOsmAuthorized(ctx))
+ {
+ intentOsm = new Intent(ctx.getPackageName() + ".OSM_AUTHORIZE");
+ intentOsm.setData(Uri.parse("gpslogger://authorize"));
+ }
+ else
+ {
+ intentOsm = new Intent(ctx.getPackageName() + ".OSM_SETUP");
+
+ }
+
+ return intentOsm;
+ }
+
+
+ public static OAuthConsumer GetOSMAuthConsumer(Context ctx)
+ {
+
+ OAuthConsumer consumer = null;
+
+ try
+ {
+ int osmConsumerKey = ctx.getResources().getIdentifier(
+ "osm_consumerkey", "string", ctx.getPackageName());
+ int osmConsumerSecret = ctx.getResources().getIdentifier(
+ "osm_consumersecret", "string", ctx.getPackageName());
+ consumer = new CommonsHttpOAuthConsumer(
+ ctx.getString(osmConsumerKey),
+ ctx.getString(osmConsumerSecret));
+
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(ctx);
+ String osmAccessToken = prefs.getString("osm_accesstoken", "");
+ String osmAccessTokenSecret = prefs.getString(
+ "osm_accesstokensecret", "");
+
+ if (osmAccessToken != null && osmAccessToken.length() > 0
+ && osmAccessTokenSecret != null
+ && osmAccessTokenSecret.length() > 0)
+ {
+ consumer.setTokenWithSecret(osmAccessToken,
+ osmAccessTokenSecret);
+ }
+
+ }
+ catch (Exception e)
+ {
+ //Swallow the exception
+ }
+
+ return consumer;
+ }
+
+
+ public void UploadGpsTrace(String fileName)
+ {
+
+ File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger");
+ File chosenFile = new File(gpxFolder, fileName);
+ OAuthConsumer consumer = GetOSMAuthConsumer(ctx);
+ String gpsTraceUrl = ctx.getString(R.string.osm_gpstrace_url);
+
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
+ String description = prefs.getString("osm_description", "");
+ String tags = prefs.getString("osm_tags", "");
+ String visibility = prefs.getString("osm_visibility", "private");
+
+ Thread t = new Thread(new OsmUploadHandler(this, consumer, gpsTraceUrl, chosenFile, description, tags, visibility));
+ t.start();
+ }
+
+ public void OnComplete()
+ {
+ callback.OnComplete();
+ }
+
+ public void OnFailure()
+ {
+ callback.OnFailure();
+ }
+
+
+ private class OsmUploadHandler implements Runnable
+ {
+ OAuthConsumer consumer;
+ String gpsTraceUrl;
+ File chosenFile;
+ String description;
+ String tags;
+ String visibility;
+ IActionListener helper;
+
+ public OsmUploadHandler(IActionListener helper, OAuthConsumer consumer, String gpsTraceUrl, File chosenFile, String description, String tags, String visibility)
+ {
+ this.consumer = consumer;
+ this.gpsTraceUrl = gpsTraceUrl;
+ this.chosenFile = chosenFile;
+ this.description = description;
+ this.tags = tags;
+ this.visibility = visibility;
+ this.helper = helper;
+ }
+
+ public void run()
+ {
+ try
+ {
+ HttpPost request = new HttpPost(gpsTraceUrl);
+
+ consumer.sign(request);
+
+ MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
+
+ FileBody gpxBody = new FileBody(chosenFile);
+
+ entity.addPart("file", gpxBody);
+ if (description == null || description.length() <= 0)
+ {
+ description = "GPSLogger for Android";
+ }
+
+ entity.addPart("description", new StringBody(description));
+ entity.addPart("tags", new StringBody(tags));
+ entity.addPart("visibility", new StringBody(visibility));
+
+ request.setEntity(entity);
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+
+ HttpResponse response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ Utilities.LogDebug("OSM Upload - " + String.valueOf(statusCode));
+ helper.OnComplete();
+
+ }
+ catch (Exception e)
+ {
+ helper.OnFailure();
+ Utilities.LogError("OsmUploadHelper.run", e);
+ }
+ }
+ }
+
+}
+
+
@@ -4,6 +4,8 @@
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
+import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
@@ -17,12 +19,13 @@
private String mCallbackUrl = "osm-login://yaoha.org";
public OAuthHelper(){
- OSMconsumer = new DefaultOAuthConsumer("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
- OSMprovider = new DefaultOAuthProvider(
- "http://www.openstreetmap.org/oauth/request_token"/*+ URLEncoder.encode(scope, "utf-8")*/,
+ this.OSMconsumer = new CommonsHttpOAuthConsumer("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
+ this.OSMprovider = new CommonsHttpOAuthProvider(
+ "http://www.openstreetmap.org/oauth/request_token",
"http://www.openstreetmap.org/oauth/access_token",
"http://www.openstreetmap.org/oauth/authorize");
- OSMprovider.setOAuth10a(true);
+ OSMconsumer.setTokenWithSecret("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
+ this.OSMprovider.setOAuth10a(true);
//mCallbackUrl = (callbackUrl == null ? OAuth.OUT_OF_BAND : callbackUrl);
}
public String getRequestToken() throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {
@@ -31,7 +34,7 @@ public String getRequestToken() throws OAuthMessageSignerException, OAuthNotAuth
}
public String[] getAccessToken(String verifier) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {
- OSMprovider.retrieveAccessToken(OSMconsumer, verifier);
+ this.OSMprovider.retrieveAccessToken(this.OSMconsumer, verifier);
return new String[] {OSMconsumer.getToken(), OSMconsumer.getTokenSecret()};
}
}
@@ -0,0 +1,72 @@
+package org.yaoha;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+
+import android.content.Intent;
+import android.net.Uri;
+import android.widget.Toast;
+
+import oauth.signpost.OAuth;
+import oauth.signpost.OAuthConsumer;
+import oauth.signpost.OAuthProvider;
+import oauth.signpost.basic.DefaultOAuthConsumer;
+import oauth.signpost.basic.DefaultOAuthProvider;
+
+public class OAuthOSM {
+
+
+
+
+
+ private static final String PASSWORD = null;
+ private static final String USERNAME = null;
+
+ public static void main(String[] args) throws Exception {
+
+ OAuthConsumer consumer = new DefaultOAuthConsumer(USERNAME, PASSWORD);
+
+// String scope = "http://www.blogger.com/feeds";
+ OAuthProvider provider = new DefaultOAuthProvider(
+ "http://www.openstreetmap.org/oauth/request_token"/*+ URLEncoder.encode(scope, "utf-8")*/,
+ "http://www.openstreetmap.org/oauth/access_token",
+ "http://www.openstreetmap.org/oauth/authorize");
+
+ Toast.makeText(this, "Fetching request token...", Toast.LENGTH_SHORT).show();
+
+ String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
+
+ System.out.println("Request token: " + consumer.getToken());
+ System.out.println("Token secret: " + consumer.getTokenSecret());
+
+
+
+ Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(authUrl));
+ startActivity(viewIntent);
+
+// System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization");
+// System.out.println("Enter the verification code and hit ENTER when you're done:");
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+ String verificationCode = br.readLine();
+
+ System.out.println("Fetching access token...");
+
+ provider.retrieveAccessToken(consumer, verificationCode.trim());
+
+ System.out.println("Access token: " + consumer.getToken());
+ System.out.println("Token secret: " + consumer.getTokenSecret());
+
+ URL url = new URL("http://openstreetmap.de/karte.html");
+ HttpURLConnection request = (HttpURLConnection) url.openConnection();
+
+ consumer.sign(request);
+
+ System.out.println("Sending request...");
+ request.connect();
+
+ System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage());
+ }
+}
Oops, something went wrong.

0 comments on commit a36d1df

Please sign in to comment.