Last active
June 22, 2016 02:58
-
-
Save rye761/708a652398201d57525cf857fc1b7d3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.github.scribejava.core.builder.ServiceBuilder; | |
import com.github.scribejava.core.model.OAuth2AccessToken; | |
import com.github.scribejava.core.model.OAuthRequest; | |
import com.github.scribejava.core.model.Response; | |
import com.github.scribejava.core.model.Verb; | |
import com.github.scribejava.core.oauth.OAuth20Service; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class TestOAuth { | |
private static final String PROTECTED_RESOURCE_URL = "https://api.unsplash.com/me"; | |
public static void main(String[] args) throws IOException { | |
final OAuth20Service service = new ServiceBuilder() | |
.apiKey("your_apikey") | |
.apiSecret("your_apisecret") | |
.scope("read_user") | |
.callback("urn:ietf:wg:oauth:2.0:oob") | |
.build(UnsplashApi.instance()); | |
final Scanner in = new Scanner(System.in); | |
System.out.println("Hello World!~Twitter checkin in"); | |
System.out.println(); | |
System.out.println("Authorize here:"); | |
System.out.println(service.getAuthorizationUrl()); | |
System.out.println("Past here:"); | |
final String oauthVerifier = in.nextLine(); | |
System.out.println(); | |
// Trade the Req Tokenn and Verifier for Access Token | |
System.out.println("Trading req for access"); | |
OAuth2AccessToken accessToken = null; | |
try { | |
accessToken = service.getAccessToken(oauthVerifier); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Got the Access Token!"); | |
System.out.println("(if your curious it looks like this: " + accessToken | |
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')"); | |
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..."); | |
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service); | |
service.signRequest(accessToken, request); | |
final Response response = request.send(); | |
System.out.println("Got it! Lets see what we found..."); | |
System.out.println(); | |
System.out.println(response.getBody()); | |
System.out.println(); | |
System.out.println("That's it man! Go and build something awesome with ScribeJava! :)"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.github.scribejava.apis.PinterestApi; | |
import com.github.scribejava.core.builder.api.DefaultApi20; | |
import com.github.scribejava.core.model.OAuthConfig; | |
import com.github.scribejava.core.model.OAuthConstants; | |
import com.github.scribejava.core.model.ParameterList; | |
/** | |
* Created by ryan on 6/17/16. | |
*/ | |
public class UnsplashApi extends DefaultApi20 { | |
protected UnsplashApi() { | |
} | |
private static class InstanceHolder { | |
private static final UnsplashApi INSTANCE = new UnsplashApi(); | |
} | |
public static UnsplashApi instance() { | |
return InstanceHolder.INSTANCE; | |
} | |
@Override | |
public String getAccessTokenEndpoint() { | |
return "https://unsplash.com/oauth/token"; | |
} | |
@Override | |
protected String getAuthorizationBaseUrl() { | |
return "https://unsplash.com/oauth/authorize"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment