2
我有一些與dropox交互的代碼。那就是:抄寫OAuth Java Google日曆api。 NO客戶端庫
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);
}
}
(此代碼工作完全BTW)
我很痛苦知道的是,我能否做類似的事情與使用劃線谷歌的API。問題是我找不到NO APP_KEY或APP_ID。我在Google Developers Console中創建了一個項目,憑證OAUTH作爲Web App,並且沒有這樣的APP密鑰或ID。
我想製作一個應用程序,它將在Web服務器(tomcat)中運行,並且用戶連接到Web服務器。所以,我已經創建了一個原生應用程序作爲Google Chrome應用程序。但是其中沒有app_key或app_secret :( – jomicobasi 2014-12-03 23:08:30
)如果您正在嘗試製作Google Chrome應用,那麼我認爲這無法使用相同的方案,請檢查:https://developers.google.com/accounts/cookbook/platforms/Chrome – 2014-12-03 23:41:15
如果爲Web應用程序生成客戶端密鑰和客戶端密鑰,您可以獲得客戶端密碼。 – 2014-12-03 23:44:57