Skip to content

Commit

Permalink
Merge core project branch 'upstream/master' into google2api
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsmirnov committed Jun 20, 2012
2 parents a8b2be7 + 2f55386 commit 3efd39b
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 38 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Expand Up @@ -82,3 +82,7 @@
* FEATURE: Kaixin2, SinaWeibo2 and Renren Apis * FEATURE: Kaixin2, SinaWeibo2 and Renren Apis
* FEATURE: ImgUr Api * FEATURE: ImgUr Api
* FEATURE: Freelancer Api (thanks Juan Palacios!) * FEATURE: Freelancer Api (thanks Juan Palacios!)
* FIX: Allow digits in url schemes
* FEATURE: Specific exception for connection problems (OAuthConnectionException)
* FIX: Dropbox Api and Evernote Api updated to their latests versions
* FIX: Digg and Skyrock Apis
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<groupId>org.scribe</groupId> <groupId>org.scribe</groupId>
<artifactId>scribe</artifactId> <artifactId>scribe</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.3.0</version> <version>1.3.1</version>
<name>Scribe OAuth Library</name> <name>Scribe OAuth Library</name>
<description>The best OAuth library out there</description> <description>The best OAuth library out there</description>
<url>http://github.com/fernandezpablo85/scribe-java</url> <url>http://github.com/fernandezpablo85/scribe-java</url>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/scribe/builder/api/DiggApi.java
@@ -0,0 +1,29 @@
package org.scribe.builder.api;

import org.scribe.model.*;

public class DiggApi extends DefaultApi10a
{

private static final String AUTHORIZATION_URL = "http://digg.com/oauth/authorize?oauth_token=%s";
private static final String BASE_URL = "http://services.digg.com/oauth/";

@Override
public String getRequestTokenEndpoint()
{
return BASE_URL + "request_token";
}

@Override
public String getAccessTokenEndpoint()
{
return BASE_URL + "access_token";
}

@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(AUTHORIZATION_URL, requestToken.getToken());
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/scribe/builder/api/DropBoxApi.java
Expand Up @@ -7,19 +7,19 @@ public class DropBoxApi extends DefaultApi10a
@Override @Override
public String getAccessTokenEndpoint() public String getAccessTokenEndpoint()
{ {
return "https://api.dropbox.com/0/oauth/access_token"; return "https://api.dropbox.com/1/oauth/access_token";
} }


@Override @Override
public String getAuthorizationUrl(Token requestToken) public String getAuthorizationUrl(Token requestToken)
{ {
return "https://www.dropbox.com/0/oauth/authorize?oauth_token="+requestToken.getToken(); return "https://www.dropbox.com/1/oauth/authorize?oauth_token="+requestToken.getToken();
} }


@Override @Override
public String getRequestTokenEndpoint() public String getRequestTokenEndpoint()
{ {
return "https://api.dropbox.com/0/oauth/request_token"; return "https://api.dropbox.com/1/oauth/request_token";
} }


} }
31 changes: 9 additions & 22 deletions src/main/java/org/scribe/builder/api/EvernoteApi.java
@@ -1,34 +1,21 @@
package org.scribe.builder.api; package org.scribe.builder.api;


import org.scribe.model.Token; import org.scribe.model.Token;
import org.scribe.model.Verb;


public class EvernoteApi extends DefaultApi10a public class EvernoteApi extends DefaultApi10a
{ {
private static final String AUTHORIZATION_URL = "https://www.evernote.com/OAuth.action?oauth_token=%s"; private static final String AUTHORIZATION_URL = "https://www.evernote.com/OAuth.action?oauth_token=%s";

@Override
public Verb getRequestTokenVerb()
{
return Verb.GET;
}


@Override @Override
public String getRequestTokenEndpoint() public String getRequestTokenEndpoint()
{ {
return "https://www.evernote.com/oauth"; return "https://www.evernote.com/oauth";
} }

@Override
public Verb getAccessTokenVerb()
{
return Verb.GET;
}


@Override @Override
public String getAccessTokenEndpoint() public String getAccessTokenEndpoint()
{ {
return "https://www.evernote.com/oauth"; return "https://www.evernote.com/oauth";
} }


@Override @Override
Expand All @@ -39,24 +26,24 @@ public String getAuthorizationUrl(Token requestToken)


public static class Sandbox extends EvernoteApi public static class Sandbox extends EvernoteApi
{ {
private static final String SANDBOX_URL = "https://sandbox.evernote.com/oauth"; private static final String SANDBOX_URL = "https://sandbox.evernote.com";


@Override @Override
public String getRequestTokenEndpoint() public String getRequestTokenEndpoint()
{ {
return SANDBOX_URL; return SANDBOX_URL + "/oauth";
} }


@Override @Override
public String getAccessTokenEndpoint() public String getAccessTokenEndpoint()
{ {
return SANDBOX_URL; return SANDBOX_URL + "/oauth";
} }


@Override @Override
public String getAuthorizationUrl(Token requestToken) public String getAuthorizationUrl(Token requestToken)
{ {
return String.format(SANDBOX_URL + "?oauth_token=%s", requestToken.getToken()); return String.format(SANDBOX_URL + "/OAuth.action?oauth_token=%s", requestToken.getToken());
} }
} }
} }
35 changes: 35 additions & 0 deletions src/main/java/org/scribe/builder/api/SkyrockApi.java
@@ -0,0 +1,35 @@
package org.scribe.builder.api;

import org.scribe.model.*;

/**
* OAuth API for Skyrock.
*
* @author Nicolas Quiénot
* @see <a href="http://www.skyrock.com/developer/">Skyrock.com API</a>
*/
public class SkyrockApi extends DefaultApi10a
{
private static final String API_ENDPOINT = "https://api.skyrock.com/v2";
private static final String REQUEST_TOKEN_RESOURCE = "/oauth/initiate";
private static final String AUTHORIZE_URL = "/oauth/authorize?oauth_token=%s";
private static final String ACCESS_TOKEN_RESOURCE = "/oauth/token";

@Override
public String getAccessTokenEndpoint()
{
return API_ENDPOINT + ACCESS_TOKEN_RESOURCE;
}

@Override
public String getRequestTokenEndpoint()
{
return API_ENDPOINT + REQUEST_TOKEN_RESOURCE;
}

@Override
public String getAuthorizationUrl(Token requestToken)
{
return String.format(API_ENDPOINT + AUTHORIZE_URL, requestToken.getToken());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/scribe/builder/api/YammerApi.java
Expand Up @@ -5,7 +5,7 @@


public class YammerApi extends DefaultApi10a public class YammerApi extends DefaultApi10a
{ {
private static final String AUTHORIZATION_URL = "'https://www.yammer.com/oauth/authorize?oauth_token=%s'"; private static final String AUTHORIZATION_URL = "https://www.yammer.com/oauth/authorize?oauth_token=%s";


@Override @Override
public String getRequestTokenEndpoint() public String getRequestTokenEndpoint()
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/scribe/exceptions/OAuthConnectionException.java
@@ -0,0 +1,14 @@
package org.scribe.exceptions;

/**
* @author: Pablo Fernandez
*/
public class OAuthConnectionException extends OAuthException
{
private static final String MSG = "There was a problem while creating a connection to the remote service.";

public OAuthConnectionException(Exception e)
{
super(MSG, e);
}
}
Expand Up @@ -8,6 +8,7 @@
public class OAuthSignatureException extends OAuthException public class OAuthSignatureException extends OAuthException
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String MSG = "Error while signing string: %s";


/** /**
* Default constructor * Default constructor
Expand All @@ -17,7 +18,7 @@ public class OAuthSignatureException extends OAuthException
*/ */
public OAuthSignatureException(String stringToSign, Exception e) public OAuthSignatureException(String stringToSign, Exception e)
{ {
super("Error while signing string: " + stringToSign, e); super(String.format(MSG, stringToSign), e);
} }


} }
Expand Up @@ -16,7 +16,7 @@
public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor public class TokenExtractorImpl implements RequestTokenExtractor, AccessTokenExtractor
{ {
private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)"); private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)");
private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]+)"); private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]*)");


/** /**
* {@inheritDoc} * {@inheritDoc}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/scribe/model/Request.java
Expand Up @@ -61,13 +61,9 @@ public Response send()
createConnection(); createConnection();
return doSend(); return doSend();
} }
catch (UnknownHostException uhe) catch (Exception e)
{ {
throw new OAuthException("Could not reach the desired host. Check your network connection.", uhe); throw new OAuthConnectionException(e);
}
catch (IOException ioe)
{
throw new OAuthException("Problems while creating connection.", ioe);
} }
} }


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/scribe/utils/Preconditions.java
Expand Up @@ -12,7 +12,9 @@
public class Preconditions public class Preconditions
{ {
private static final String DEFAULT_MESSAGE = "Received an invalid parameter"; private static final String DEFAULT_MESSAGE = "Received an invalid parameter";
private static final Pattern URL_PATTERN = Pattern.compile("[a-zA-Z_-]+://\\S+");
// scheme = alpha *( alpha | digit | "+" | "-" | "." )
private static final Pattern URL_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*://\\S+");


/** /**
* Checks that an object is not null. * Checks that an object is not null.
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/org/scribe/examples/DiggExample.java
@@ -0,0 +1,65 @@
package org.scribe.examples;

import java.util.*;

import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;

public class DiggExample
{
private static final String NETWORK_NAME = "Digg";
private static final String PROTECTED_RESOURCE_URL = "http://services.digg.com/2.0/comment.digg";

public static void main(String[] args)
{
// Replace these with your own api key and secret
String apiKey = "myKey";
String apiSecret = "mySecret";
OAuthService service = new ServiceBuilder().provider(DiggApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
Scanner in = new Scanner(System.in);

System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();

// Obtain the Request Token
System.out.println("Fetching the Request Token...");
Token requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();

// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
String authorizationUrl = service.getAuthorizationUrl(requestToken);
System.out.println("Got the Authorization URL!");
System.out.println("Now go and authorize Scribe here:");
System.out.println(authorizationUrl);
System.out.println("And paste the authorization code here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();

// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
Token accessToken = service.getAccessToken(requestToken, verifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: " + accessToken + " )");
System.out.println();

// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
request.addBodyParameter("comment_id", "20100729223726:4fef610331ee46a3b5cbd740bf71313e");
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());

System.out.println();
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");

}
}
4 changes: 2 additions & 2 deletions src/test/java/org/scribe/examples/SinaWeibo2Example.java
Expand Up @@ -15,8 +15,8 @@ public class SinaWeibo2Example
public static void main(String[] args) public static void main(String[] args)
{ {
// Replace these with your own api key and secret // Replace these with your own api key and secret
String apiKey = "342348223"; String apiKey = "your_api_key";
String apiSecret = "cfdf672e166a4bc954c0e33f03cf0d1b"; String apiSecret = "your_api_secret";
OAuthService service = new ServiceBuilder() OAuthService service = new ServiceBuilder()
.provider(SinaWeiboApi20.class) .provider(SinaWeiboApi20.class)
.apiKey(apiKey) .apiKey(apiKey)
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/scribe/examples/SkyrockExample.java
@@ -0,0 +1,61 @@
package org.scribe.examples;

import java.util.Scanner;

import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.oauth.*;

public class SkyrockExample
{
private static final String PROTECTED_RESOURCE_URL = "https://api.skyrock.com/v2/user/get.json";

public static void main(String[] args)
{
OAuthService service = new ServiceBuilder()
.provider(SkyrockApi.class)
.apiKey("your-api-key")
.apiSecret("your-api-secret")
.build();
Scanner in = new Scanner(System.in);

System.out.println("=== Skyrock's OAuth Workflow ===");
System.out.println();

// Obtain the Request Token
System.out.println("Fetching the Request Token...");
Token requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();

System.out.println("Now go and authorize Scribe here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();

// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
Token accessToken = service.getAccessToken(requestToken, verifier);
System.out.println("Got the Access Token!");
System.out.println("(if your curious it looks like this: " + accessToken + " )");
System.out.println();

// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getCode());
System.out.println(response.getBody());

System.out.println();
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");

}

}

0 comments on commit 3efd39b

Please sign in to comment.