|
| 1 | +package org.scribe.examples; |
| 2 | + |
| 3 | +import org.scribe.builder.*; |
| 4 | +import org.scribe.builder.api.*; |
| 5 | +import org.scribe.model.*; |
| 6 | +import org.scribe.oauth.*; |
| 7 | +import java.nio.charset.*; |
| 8 | +import java.security.*; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +public class RenrenExample |
| 12 | +{ |
| 13 | + private static final String NETWORK_NAME = "Renren"; |
| 14 | + private static final String PROTECTED_RESOURCE_URL = "http://api.renren.com/restserver.do"; |
| 15 | + private static final Token EMPTY_TOKEN = null; |
| 16 | + |
| 17 | + public static void main(String[] args) |
| 18 | + { |
| 19 | + // Replace these with your own api key and secret |
| 20 | + String apiKey = "your api key"; |
| 21 | + String apiSecret = "your api secret"; |
| 22 | + OAuthService service = new ServiceBuilder() |
| 23 | + .provider(RenrenApi.class) |
| 24 | + .apiKey(apiKey) |
| 25 | + .apiSecret(apiSecret) |
| 26 | + .scope("status_update publish_feed") |
| 27 | + .callback("http://your.doman.com/oauth/renren") |
| 28 | + .build(); |
| 29 | + Scanner in = new Scanner(System.in); |
| 30 | + |
| 31 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 32 | + System.out.println(); |
| 33 | + |
| 34 | + // Obtain the Authorization URL |
| 35 | + System.out.println("Fetching the Authorization URL..."); |
| 36 | + String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN); |
| 37 | + System.out.println("Got the Authorization URL!"); |
| 38 | + System.out.println("Now go and authorize Scribe here:"); |
| 39 | + System.out.println(authorizationUrl); |
| 40 | + System.out.println("And paste the authorization code here"); |
| 41 | + System.out.print(">>"); |
| 42 | + Verifier verifier = new Verifier(in.nextLine()); |
| 43 | + System.out.println(); |
| 44 | + |
| 45 | + // Trade the Request Token and Verfier for the Access Token |
| 46 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 47 | + Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier); |
| 48 | + System.out.println("Got the Access Token!"); |
| 49 | + System.out.println("(if your curious it looks like this: " + accessToken + " )"); |
| 50 | + System.out.println(); |
| 51 | + |
| 52 | + // Now let's go and ask for a protected resource! |
| 53 | + System.out.println("Now we're going to access a protected resource..."); |
| 54 | + OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL); |
| 55 | + Map<String, String> parameters = new HashMap<String, String>(); |
| 56 | + parameters.put("method", "users.getInfo"); |
| 57 | + parameters.put("format", "json"); |
| 58 | + parameters.put("v", "1.0"); |
| 59 | + |
| 60 | + List<String> sigString = new ArrayList<String>(parameters.size() + 1); |
| 61 | + for (Map.Entry<String, String> entry : parameters.entrySet()) |
| 62 | + { |
| 63 | + request.addQuerystringParameter(entry.getKey(), entry.getValue()); |
| 64 | + sigString.add(String.format("%s=%s", entry.getKey(), entry.getValue())); |
| 65 | + } |
| 66 | + sigString.add(String.format("%s=%s", OAuthConstants.ACCESS_TOKEN, accessToken.getToken())); |
| 67 | + Collections.sort(sigString); |
| 68 | + StringBuilder b = new StringBuilder(); |
| 69 | + for (String param : sigString) |
| 70 | + { |
| 71 | + b.append(param); |
| 72 | + } |
| 73 | + b.append(apiSecret); |
| 74 | + System.out.println("Sig string: " + b.toString()); |
| 75 | + request.addQuerystringParameter("sig", md5(b.toString())); |
| 76 | + service.signRequest(accessToken, request); |
| 77 | + Response response = request.send(); |
| 78 | + System.out.println("Got it! Lets see what we found..."); |
| 79 | + System.out.println(); |
| 80 | + System.out.println(response.getCode()); |
| 81 | + System.out.println(response.getBody()); |
| 82 | + |
| 83 | + System.out.println(); |
| 84 | + System.out.println("Thats it man! Go and build something awesome with Scribe! :)"); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + public static String md5(String orgString) |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + java.security.MessageDigest md = MessageDigest.getInstance("MD5"); |
| 93 | + byte[] array = md.digest(orgString.getBytes(Charset.forName("UTF-8"))); |
| 94 | + StringBuffer sb = new StringBuffer(); |
| 95 | + for (int i = 0; i < array.length; ++i) |
| 96 | + { |
| 97 | + sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3)); |
| 98 | + } |
| 99 | + return sb.toString(); |
| 100 | + } |
| 101 | + catch (NoSuchAlgorithmException e) |
| 102 | + { |
| 103 | + e.printStackTrace(); |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments