2

I've some code for interacting with dropox. Here it is:

package rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.DropBoxApi;
import org.scribe.exceptions.OAuthException;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

// Step 1: Create Dropbox Account
// Step 2: Create Application (https://www.dropbox.com/developers)

public class DropBoxRestClient {
    // Access codes #1: per application used to get access codes #2 
    private static final String API_APP_KEY = "NOT_OF_YOUR_CONCERN";
    private static final String API_APP_SECRET = "NOT_OF_YOUR_CONCERN";

    // Access codes #2: per user per application
    private static final String API_USER_TOKEN = "NOT_OF_YOUR_CONCERN";
    private static final String API_USER_SECRET = "NOT_OF_YOUR_CONCERN";

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        OAuthService service = new ServiceBuilder()
                                .provider(DropBoxApi.class)
                                .apiKey(API_APP_KEY)
                                .apiSecret(API_APP_SECRET)
                                .build();

        try {
            if ( API_USER_TOKEN.equals("") || API_USER_SECRET.equals("") ) {
                System.out.println("Fetching the Request Token...");
                Token requestToken = service.getRequestToken();
                System.out.println("Now go and authorize Scribe here:");
                System.out.println(service.getAuthorizationUrl(requestToken));
                System.out.println("Press enter when done.");
                System.out.print(">>");
                Verifier verifier = new Verifier(in.nextLine());
                Token accessToken = service.getAccessToken(requestToken, verifier);
                System.out.println("Define API_USER_TOKEN: " + accessToken.getToken());
                System.out.println("Define API_USER_SECRET: " + accessToken.getSecret());
                System.exit(0);
            }

            Token accessToken = new Token( API_USER_TOKEN, API_USER_SECRET);

            listFiles(service, accessToken);
            addFile("Hangover.txt", service, accessToken);
            //listFiles(service, accessToken);
            //deleteFile("teste.txt", service, accessToken);
            //listFiles(service, accessToken);

        } catch(ParseException e) {
            e.printStackTrace();
        } catch(OAuthException e) {
            e.printStackTrace();
        }
    }

    private static void listFiles(OAuthService service, Token accessToken) throws ParseException {
        OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.dropbox.com/1/metadata/dropbox/");
        request.addQuerystringParameter("list", "true");
        service.signRequest(accessToken, request);
        Response response = request.send();

        System.out.println("Got it! Lets see what we found...");
        System.out.println("HTTP RESPONSE: =============");
        System.out.println(response.getCode());

        JSONParser parser = new JSONParser();

        String jsonAll = response.getBody();
        Object obj = parser.parse(jsonAll);

        JSONObject jsonAllObj = (JSONObject) obj;

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonItem = gson.toJson(jsonAllObj);

        System.out.println(jsonItem);
        System.out.println("END RESPONSE ===============");

        JSONObject rj = (JSONObject) JSONValue.parse(response.getBody());
        JSONArray contents = (JSONArray) rj.get("contents");
        for (int i=0; i<contents.size(); i++) {
            JSONObject item = (JSONObject) contents.get(i);
            String path = (String) item.get("path");
            System.out.println(" - " + path);
        }
    }

    private static void addFile(String path, OAuthService service, Token accessToken) throws ParseException {
        //TODO
        OAuthRequest request = new OAuthRequest(Verb.PUT, 
                "https://api-content.dropbox.com/1/files_put/auto/" + path);

        request.addHeader("Content-Type", "text/plain");
        request.addPayload("I love Cocain!");
        service.signRequest(accessToken, request);
        Response response = request.send();

        System.out.println("Is File Created? Lets see what we found...");
        System.out.println("HTTP RESPONSE: =============");
        System.out.println(response.getCode());
        System.out.println(response.getBody());
        System.out.println("END RESPONSE ===============");

        JSONParser parser = new JSONParser();

        String jsonAll = response.getBody();
        Object obj = parser.parse(jsonAll);

        JSONObject jsonAllObj = (JSONObject) obj;

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonItem = gson.toJson(jsonAllObj);

        System.out.println(jsonItem);
    }

    private static void deleteFile(String path, OAuthService service, Token accessToken) throws ParseException {
        //TODO
        OAuthRequest request = new OAuthRequest(Verb.POST, 
                "https://api.dropbox.com/1/fileops/delete");
        request.addQuerystringParameter("root", "auto");
        request.addQuerystringParameter("path", path);
        service.signRequest(accessToken, request);
        Response response = request.send();

        System.out.println("Is File Created? Lets see what we found...");
        System.out.println("HTTP RESPONSE: =============");
        System.out.println(response.getCode());
        System.out.println(response.getBody());
        System.out.println("END RESPONSE ===============");

        JSONParser parser = new JSONParser();

        String jsonAll = response.getBody();
        Object obj = parser.parse(jsonAll);

        JSONObject jsonAllObj = (JSONObject) obj;

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonItem = gson.toJson(jsonAllObj);

        System.out.println(jsonItem);
    }
}

(This code works perfectly BTW)
What I am in pain to know is whether I can do a similar thing with google APIs using scribe. The thing is I can't find NO APP_KEY or APP_ID. I've created a project in Google Developers Console with credentials OAUTH as Web App and there are no such APP keys or IDs.

Etheryte
  • 24,589
  • 11
  • 71
  • 116
jomicobasi
  • 117
  • 1
  • 2
  • 11

1 Answers1

2

You are almost there, from https://developers.google.com/console/help/new/#generatingoauth2

Go to the Google Developers Console.
Select a project, or create a new one.
In the sidebar on the left, expand APIs & auth. Next, click APIs. In the list of APIs, make sure all of the APIs you are using show a status of ON.
In the sidebar on the left, select Credentials.
Click Create new Client ID.
Select the appropriate Application Type for your project and enter any additional information required.

Leandro Carracedo
  • 7,233
  • 2
  • 44
  • 50