Skip to content

Commit 990794d

Browse files
committed
Merge branch 'dropboxapi' of https://github.com/petrkopotev/scribejava into petrkopotev-dropboxapi
2 parents c38249d + cafa542 commit 990794d

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
5+
/**
6+
* Dropbox.com Api
7+
*/
8+
public class DropboxApi extends DefaultApi20 {
9+
10+
protected DropboxApi() {
11+
}
12+
13+
private static class InstanceHolder {
14+
private static final DropboxApi INSTANCE = new DropboxApi();
15+
}
16+
17+
public static DropboxApi instance() {
18+
return InstanceHolder.INSTANCE;
19+
}
20+
21+
@Override
22+
public String getAccessTokenEndpoint() {
23+
return "https://api.dropbox.com/oauth2/token";
24+
}
25+
26+
@Override
27+
protected String getAuthorizationBaseUrl() {
28+
return "https://www.dropbox.com/oauth2/authorize";
29+
}
30+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.DropboxApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.model.Response;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.oauth.OAuth20Service;
10+
11+
import java.io.IOException;
12+
import java.util.Scanner;
13+
import java.util.concurrent.ExecutionException;
14+
15+
public class DropboxExample {
16+
17+
private static final String NETWORK_NAME = "Dropbox.com";
18+
private static final String PROTECTED_RESOURCE_URL
19+
= "https://api.dropboxapi.com/2/users/get_space_usage";
20+
private static final String PAYLOAD = "null";
21+
private static final String CONTENT_TYPE_NAME = "Content-Type";
22+
private static final String CONTENT_TYPE_VALUE = "application/json";
23+
24+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
25+
// Replace these with your client id and secret
26+
final String clientId = "client id";
27+
final String clientSecret = "client secret";
28+
final OAuth20Service service = new ServiceBuilder(clientId)
29+
.apiSecret(clientSecret)
30+
.callback("https://www.example.com/oauth_callback/")
31+
.build(DropboxApi.instance());
32+
33+
final Scanner in = new Scanner(System.in);
34+
35+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
36+
System.out.println();
37+
38+
// Obtain the Authorization URL
39+
System.out.println("Fetching the Authorization URL...");
40+
final String authorizationUrl = service.getAuthorizationUrl();
41+
System.out.println("Got the Authorization URL!");
42+
System.out.println("Now go and authorize ScribeJava here:");
43+
System.out.println(authorizationUrl);
44+
System.out.println("And paste the authorization code here");
45+
System.out.print(">>");
46+
final String code = in.nextLine();
47+
System.out.println();
48+
49+
System.out.println("Trading the Authorization Code for an Access Token...");
50+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
51+
System.out.println("Got the Access Token!");
52+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
53+
System.out.println();
54+
55+
System.out.println("Now we're going to access a protected resource...");
56+
final OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
57+
request.addHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE);
58+
request.setPayload(PAYLOAD);
59+
service.signRequest(accessToken, request);
60+
61+
final Response response = service.execute(request);
62+
System.out.println("Got it! Lets see what we found...");
63+
System.out.println();
64+
System.out.println(response.getCode());
65+
System.out.println(response.getBody());
66+
67+
System.out.println();
68+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
69+
}
70+
}

0 commit comments

Comments
 (0)