Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changed package name

  • Loading branch information...
commit 3fbc6ee5f6cf12481a373c6eb714937f2114672a 1 parent d525883
unknown authored
View
2  OauthApp/PlaxoApp/AndroidManifest.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.barryku.android.plaxo"
+ package="com.barryku.android.plaxo2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
View
38 OauthApp/PlaxoApp/gen/com/barryku/android/plaxo/R.java
@@ -1,38 +0,0 @@
-/* AUTO-GENERATED FILE. DO NOT MODIFY.
- *
- * This class was automatically generated by the
- * aapt tool from the resource data it found. It
- * should not be modified by hand.
- */
-
-package com.barryku.android.plaxo;
-
-public final class R {
- public static final class attr {
- }
- public static final class drawable {
- public static final int icon=0x7f020000;
- public static final int icon_authorize=0x7f020001;
- public static final int icon_logout=0x7f020002;
- }
- public static final class id {
- public static final int authorize=0x7f060004;
- public static final int doSerarch=0x7f060002;
- public static final int logout=0x7f060005;
- public static final int result=0x7f060003;
- public static final int searchBar=0x7f060000;
- public static final int searchText=0x7f060001;
- }
- public static final class layout {
- public static final int main=0x7f030000;
- }
- public static final class menu {
- public static final int main_menu=0x7f050000;
- }
- public static final class string {
- public static final int app_name=0x7f040001;
- public static final int hello=0x7f040000;
- public static final int not_authorized=0x7f040002;
- public static final int oauth_scheme=0x7f040003;
- }
-}
View
2  OauthApp/PlaxoApp/res/menu/main_menu.xml
@@ -2,6 +2,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/authorize"
android:icon="@drawable/icon_authorize" android:title="Authorize"/>
+<item android:id="@+id/scribe_auth"
+ android:title="via Scribe"/>
<item android:id="@+id/logout"
android:icon="@drawable/icon_logout" android:title="Log out"/>
</menu>
View
6 OauthApp/PlaxoApp/res/values/strings.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PlaxoMainActivity!</string>
- <string name="app_name">Plaxo App</string>
- <string name="not_authorized">Please click menu to authorize this application to access your Plaxo data.</string>
- <string name="oauth_scheme">myoauth</string>
+ <string name="app_name">OAuth App</string>
+ <string name="not_authorized">Please click menu to authorize this application to access your data.</string>
+ <string name="oauth_scheme">cloudcontact</string>
</resources>
View
204 OauthApp/PlaxoApp/src/com/barryku/android/plaxo/PlaxoMainActivity.java
@@ -1,204 +0,0 @@
-package com.barryku.android.plaxo;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
-import org.scribe.builder.ServiceBuilder;
-import org.scribe.builder.api.PlaxoApi;
-import org.scribe.model.OAuthRequest;
-import org.scribe.model.Token;
-import org.scribe.model.Verb;
-import org.scribe.model.Verifier;
-import org.scribe.oauth.OAuthService;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.RestTemplate;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.net.Uri;
-import android.os.Bundle;
-import android.preference.PreferenceManager;
-import android.util.Base64;
-import android.util.Log;
-import android.view.Menu;
-import android.view.MenuInflater;
-import android.view.MenuItem;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.RelativeLayout;
-import android.widget.TextView;
-
-public class PlaxoMainActivity extends Activity {
- private static final String RESOURCE_URL =
- "http://www.plaxo.com/pdata/contacts/@me/@all?filterBy=displayName&filterOp=contains&filterValue=%s";
- private static final String PREF_ACCESS_TOKEN = "ACCESS_TOKEN";
- private static final String PREF_REQUEST_TOKEN = "REQUEST_TOKEN";
- private static final String LOG_TAG = "mydebug";
- private String oauthScheme;
- private OAuthService oauth;
- private SharedPreferences prefs;
- private SpringOauthService springOauth;
- private Token accessToken;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- oauthScheme = getString(R.string.oauth_scheme);
-
- setContentView(R.layout.main);
- oauth = new ServiceBuilder()
- .provider(PlaxoApi.class)
- .apiKey("anonymous")
- .apiSecret("")
- .callback(oauthScheme + "://auth?callback")
- .build();
- /**
- oauth = new ServiceBuilder()
- .provider(YahooApi.class)
- .apiKey("dj0yJmk9TXZDWVpNVVdGaVFmJmQ9WVdrOWMweHZXbkZLTkhVbWNHbzlNVEl5TWprd05qUTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wMw--")
- .apiSecret("262be559f92a2be20c4c039419018f2b48cdfce9")
- .build();
- **/
- springOauth = new SpringOauthService(oauth);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
- //handling oauth callback
- Intent intent = getIntent();
-
- if (oauthScheme.equals(intent.getScheme())) {
- Uri uri = intent.getData();
- String verifier = uri.getQueryParameter("oauth_verifier");
- if (verifier != null) {
- Token requestToken = fromString(prefs.getString(PREF_REQUEST_TOKEN, null));
- accessToken = springOauth.getAccessToken(requestToken, new Verifier(verifier));
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString(PREF_ACCESS_TOKEN, convertToString(accessToken));
- editor.commit();
- }
- }
-
- Button doSearch = (Button) findViewById(R.id.doSerarch);
- doSearch.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
- EditText searchTxt = (EditText) findViewById(R.id.searchText);
- OAuthRequest request = new OAuthRequest(Verb.GET,
- String.format(RESOURCE_URL, searchTxt.getText()));
- springOauth.signRequest(accessToken, request);
- RestTemplate rest = RestUtil.getRestTemplate();
- ResponseEntity<String> response = rest.exchange(request.getUrl(), HttpMethod.GET, springOauth.toHttpEntity(request),String.class);
- TextView result = (TextView) findViewById(R.id.result);
- result.setText(response.getBody());
- }
- });
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- String accessTokenString = prefs.getString(PREF_ACCESS_TOKEN, null);
-
- if (accessTokenString == null) {
- setSearchBarVisibility(false);
-
- } else {
- setSearchBarVisibility(true);
- accessToken = fromString(prefs.getString(PREF_ACCESS_TOKEN, null));
- Log.d(LOG_TAG, "access token: " + accessToken);
- }
- }
-
- private void setSearchBarVisibility(boolean visible){
- TextView result = (TextView) findViewById(R.id.result);
- RelativeLayout searchBar = (RelativeLayout) findViewById(R.id.searchBar);
- searchBar.setVisibility(visible?View.VISIBLE:View.INVISIBLE);
- result.setText(visible ? "" : getString(R.string.not_authorized));
- }
-
- private void setAuthMenuVisibility(boolean visible, Menu menu) {
- MenuItem authorize = menu.findItem(R.id.authorize);
- if (authorize != null) {
- MenuItem logout = menu.findItem(R.id.logout);
- authorize.setVisible(visible);
- logout.setVisible(!visible);
- }
- }
-
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- if (accessToken == null) {
- setAuthMenuVisibility(true, menu);
- } else {
- setAuthMenuVisibility(false, menu);
- }
- return super.onPrepareOptionsMenu(menu);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main_menu, menu);
- return true;
- }
-
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.authorize:
- Token requestToken = springOauth.getRequestToken();
-
- Log.d(LOG_TAG, "token: " + requestToken);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString(PREF_REQUEST_TOKEN, convertToString(requestToken));
- editor.commit();
- Intent authIntent = new Intent("android.intent.action.VIEW",
- Uri.parse(oauth.getAuthorizationUrl(requestToken)));
- startActivity(authIntent);
- break;
- case R.id.logout:
- SharedPreferences.Editor editor2 = prefs.edit();
- editor2.remove(PREF_ACCESS_TOKEN);
- editor2.remove(PREF_REQUEST_TOKEN);
- editor2.commit();
- accessToken = null;
- setSearchBarVisibility(false);
- break;
- }
- return super.onOptionsItemSelected(item);
- }
-
- private String convertToString(Token token) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
- ObjectOutputStream oos = new ObjectOutputStream( baos );
- oos.writeObject( token );
- oos.close();
- } catch (IOException ioe) {
- Log.d(LOG_TAG, "conversion failed: " + ioe.getMessage());
- }
- return Base64.encodeToString( baos.toByteArray(), Base64.DEFAULT );
- }
-
- private Token fromString(String str) {
- byte [] data = Base64.decode(str, Base64.DEFAULT );
- ObjectInputStream ois;
- Object o = null;
- try {
- ois = new ObjectInputStream(new ByteArrayInputStream( data ) );
- o = ois.readObject();
- ois.close();
- } catch (Exception e) {
- Log.d(LOG_TAG, "conversion failed: " + e.getMessage());
- }
- return (Token) o;
- }
-}
View
40 OauthApp/PlaxoApp/src/com/barryku/android/plaxo/RestUtil.java
@@ -1,40 +0,0 @@
-package com.barryku.android.plaxo;
-
-import java.io.IOException;
-import java.nio.charset.Charset;
-
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
-import org.springframework.http.converter.FormHttpMessageConverter;
-import org.springframework.web.client.HttpClientErrorException;
-import org.springframework.web.client.ResponseErrorHandler;
-import org.springframework.web.client.RestTemplate;
-
-import android.util.Log;
-
-public class RestUtil {
- public static RestTemplate getRestTemplate() {
- RestTemplate restTemplate = new RestTemplate();
- FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
- formConverter.setCharset(Charset.forName("UTF8"));
- restTemplate.getMessageConverters().add(formConverter);
-
- restTemplate.setErrorHandler(new ResponseErrorHandler() {
-
- public boolean hasError(ClientHttpResponse resp) throws IOException {
- HttpStatus status = resp.getStatusCode();
- if (HttpStatus.CREATED.equals(status) || HttpStatus.OK.equals(status)) {
- return false;
- } else {
- Log.d("mydebug", "response: " + resp.getBody());
- return true;
- }
- }
-
- public void handleError(ClientHttpResponse resp) throws IOException {
- throw new HttpClientErrorException(resp.getStatusCode());
- }
- });
- return restTemplate;
- }
-}
View
112 OauthApp/PlaxoApp/src/com/barryku/android/plaxo/SpringOauthService.java
@@ -1,112 +0,0 @@
-package com.barryku.android.plaxo;
-
-import java.util.Map;
-
-import org.scribe.builder.api.Api;
-import org.scribe.builder.api.DefaultApi10a;
-import org.scribe.model.OAuthConfig;
-import org.scribe.model.OAuthConstants;
-import org.scribe.model.OAuthRequest;
-import org.scribe.model.Token;
-import org.scribe.model.Verifier;
-import org.scribe.oauth.OAuthService;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.RestTemplate;
-
-public class SpringOauthService implements OAuthService {
- private OAuthConfig config;
- private DefaultApi10a api;
- private OAuthService oauth;
-
-
- public SpringOauthService(OAuthService oauth) {
- this.oauth = oauth;
- api = (DefaultApi10a) oauth.getApi();
- config = oauth.getConfig();
- }
- public Token getAccessToken(Token requestToken, Verifier verifier) {
- OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
- request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
- request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
- addOAuthParams(request, requestToken);
- addSignature(request);
- RestTemplate rest = RestUtil.getRestTemplate();
- ResponseEntity<String> response = rest.exchange(request.getUrl(), HttpMethod.POST, toHttpEntity(request), String.class);
- return api.getAccessTokenExtractor().extract(response.getBody());
- }
-
- public String getAuthorizationUrl(Token requestToken) {
- return oauth.getAuthorizationUrl(requestToken);
- }
-
- public Token getRequestToken() {
- OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
- request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
- addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
- addSignature(request);
- RestTemplate rest = RestUtil.getRestTemplate();
- ResponseEntity<String> response = rest.exchange(request.getUrl(), HttpMethod.POST, toHttpEntity(request),String.class);
- return api.getRequestTokenExtractor().extract(response.getBody());
- }
-
- public HttpEntity<String> toHttpEntity(OAuthRequest request) {
- HttpHeaders headers = new HttpHeaders();
- for (Map.Entry<String, String> entry: request.getHeaders().entrySet()) {
- headers.add(entry.getKey(), entry.getValue());
- }
- return new HttpEntity<String>(headers);
- }
-
- public String getVersion() {
- return oauth.getVersion();
- }
-
- public void signRequest(Token accessToken, OAuthRequest request) {
- oauth.signRequest(accessToken, request);
-
- }
- public Api getApi() {
- return api;
- }
- public OAuthConfig getConfig() {
- return config;
- }
-
- private void addOAuthParams(OAuthRequest request, Token token)
- {
- request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
- request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce());
- request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
- request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
- request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
- if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
- request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
- }
-
- private void addSignature(OAuthRequest request)
- {
- switch (config.getSignatureType())
- {
- case Header:
- String oauthHeader = api.getHeaderExtractor().extract(request);
- request.addHeader(OAuthConstants.HEADER, oauthHeader);
- break;
- case QueryString:
- for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
- {
- request.addQuerystringParameter(entry.getKey(), entry.getValue());
- }
- break;
- }
- }
-
- private String getSignature(OAuthRequest request, Token token)
- {
- String baseString = api.getBaseStringExtractor().extract(request);
- return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
- }
-
-}
View
112 OauthApp/ScribeSpring/src/main/java/com/barryku/test/SpringOauthService.java
@@ -1,112 +0,0 @@
-package com.barryku.test;
-
-import java.util.Map;
-
-import org.scribe.builder.api.Api;
-import org.scribe.builder.api.DefaultApi10a;
-import org.scribe.model.OAuthConfig;
-import org.scribe.model.OAuthConstants;
-import org.scribe.model.OAuthRequest;
-import org.scribe.model.Token;
-import org.scribe.model.Verifier;
-import org.scribe.oauth.OAuthService;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.client.RestTemplate;
-
-public class SpringOauthService implements OAuthService {
- private OAuthConfig config;
- private DefaultApi10a api;
- private OAuthService oauth;
-
-
- public SpringOauthService(OAuthService oauth) {
- this.oauth = oauth;
- api = (DefaultApi10a) oauth.getApi();
- config = oauth.getConfig();
- }
- public Token getAccessToken(Token requestToken, Verifier verifier) {
- OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
- request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
- request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
- addOAuthParams(request, requestToken);
- addSignature(request);
- RestTemplate rest = RestUtil.getRestTemplate();
- ResponseEntity<String> response = rest.exchange(request.getUrl(), HttpMethod.POST, toHttpEntity(request), String.class);
- return api.getAccessTokenExtractor().extract(response.getBody());
- }
-
- public String getAuthorizationUrl(Token requestToken) {
- return oauth.getAuthorizationUrl(requestToken);
- }
-
- public Token getRequestToken() {
- OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
- request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
- addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
- addSignature(request);
- RestTemplate rest = RestUtil.getRestTemplate();
- ResponseEntity<String> response = rest.exchange(request.getUrl(), HttpMethod.POST, toHttpEntity(request),String.class);
- return api.getRequestTokenExtractor().extract(response.getBody());
- }
-
- public HttpEntity<String> toHttpEntity(OAuthRequest request) {
- HttpHeaders headers = new HttpHeaders();
- for (Map.Entry<String, String> entry: request.getHeaders().entrySet()) {
- headers.add(entry.getKey(), entry.getValue());
- }
- return new HttpEntity<String>(headers);
- }
-
- public String getVersion() {
- return oauth.getVersion();
- }
-
- public void signRequest(Token accessToken, OAuthRequest request) {
- oauth.signRequest(accessToken, request);
-
- }
- public Api getApi() {
- return api;
- }
- public OAuthConfig getConfig() {
- return config;
- }
-
- private void addOAuthParams(OAuthRequest request, Token token)
- {
- request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
- request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce());
- request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
- request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
- request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
- if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
- request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
- }
-
- private void addSignature(OAuthRequest request)
- {
- switch (config.getSignatureType())
- {
- case Header:
- String oauthHeader = api.getHeaderExtractor().extract(request);
- request.addHeader(OAuthConstants.HEADER, oauthHeader);
- break;
- case QueryString:
- for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
- {
- request.addQuerystringParameter(entry.getKey(), entry.getValue());
- }
- break;
- }
- }
-
- private String getSignature(OAuthRequest request, Token token)
- {
- String baseString = api.getBaseStringExtractor().extract(request);
- return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
- }
-
-}
View
12 OauthApp/ScribeSpring/src/main/java/com/barryku/test/YahooExample.java
@@ -8,14 +8,18 @@
public class YahooExample
{
- private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/%s/profile/status?format=json";
-
+ //private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/%s/profile/status?format=json";
+ private static final String API_KEY = "dj0yJmk9TDMyc0prOFlFb3NuJmQ9WVdrOWVXNXFVa3hpTnpZbWNHbzlNak00TmpnME1EWXkmcz1jb25zdW1lcnNlY3JldCZ4PWVl";
+ private static final String API_SECRET = "e8fb247893d0de0a72842be2ded4a57d58520ee6";
+ private static final String PROTECTED_RESOURCE_URL = "http://social.yahooapis.com/v1/user/%s/contacts;name.contains=michael?format=json";
public static void main(String[] args)
{
+
OAuthService service = new ServiceBuilder()
.provider(SpringYahooApi.class)
- .apiKey("dj0yJmk9TXZDWVpNVVdGaVFmJmQ9WVdrOWMweHZXbkZLTkhVbWNHbzlNVEl5TWprd05qUTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0wMw--")
- .apiSecret("262be559f92a2be20c4c039419018f2b48cdfce9")
+ .apiKey(API_KEY)
+ .apiSecret(API_SECRET)
+ .callback("http://oauth.cloudfoundry.com/auth?callback")
.build();
Scanner in = new Scanner(System.in);
Please sign in to comment.
Something went wrong with that request. Please try again.