gpt4 book ai didi

java - 抄写 OAuth Java 谷歌日历 api。没有客户端库

转载 作者:行者123 更新时间:2023-11-30 08:59:26 24 4
gpt4 key购买 nike

我有一些与 dropox 交互的代码。在这里:

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);
}
}

(顺便说一句,这段代码完美运行)
我很想知道我是否可以使用 scribe 对 google API 做类似的事情。问题是我找不到 NO APP_KEY 或 APP_ID。我在 Google Developers Console 中创建了一个项目,使用凭证 OAUTH 作为 Web 应用程序,但没有这样的应用程序 key 或 ID。

最佳答案

你快到了,来自 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.

关于java - 抄写 OAuth Java 谷歌日历 api。没有客户端库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com