我一直在尝试为个人项目运行Java Google Drive API快速入门程序(https://developers.google.com/drive/api/v3/quickstart/java)。但是,我现在开始意识到本教程到目前为止到处都是错误。 Java版本已过期(1.7),必须手动更改许多“复制和粘贴”文件。无论如何,解决了上述问题后,在使用gradle -q run启动程序后,我已经从命令行的Gradle中收到以下错误消息。完整的错误消息和代码在下面,但似乎错误的两个主要部分是:
Exception in thread "main"
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<'
(code 60)): expected a valid value (number, String, array, object,
'true', 'false' or 'null')
和
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
完整错误消息:
Josephs-MacBook-Pro:Drive APP josephglusker$ gradle run
> Task :run FAILED
Exception in thread "main"
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<'
(code 60)): expected a valid value (number, String, array, object,
'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@43556938; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1378)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:599)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:520)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleUnexpectedValue(ReaderBasedJsonParser.java:1387)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:678)
at com.google.api.client.json.jackson2.JacksonParser.nextToken(JacksonParser.java:55)
at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:221)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:380)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:336)
at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:166)
at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:148)
at com.google.api.client.json.JsonFactory.fromReader(JsonFactory.java:236)
at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:192)
at DriveQuickstart.getCredentials(DriveQuickstart.java:59)
at DriveQuickstart.main(DriveQuickstart.java:73)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command'
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
3 actionable tasks: 2 executed, 1 up-to-date
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'AppsScriptQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-script:v1-rev175-1.23.0'
}
DriveQuickstart.java(主类)
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.script.Script;
import com.google.api.services.script.model.Content;
import com.google.api.services.script.model.CreateProjectRequest;
import com.google.api.services.script.model.File;
import com.google.api.services.script.model.Project;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class AppsScriptQuickstart {
private static final String APPLICATION_NAME = "Apps Script API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String CREDENTIALS_FOLDER = "credentials"; // Directory to store user credentials.
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved credentials folder at /secret.
*/
private static final List<String> SCOPES = Collections.singletonList("https://www.googleapis.com/auth/script.projects");
private static final String CLIENT_SECRET_DIR = "client_secret.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If there is no client_secret.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = AppsScriptQuickstart.class.getResourceAsStream(CLIENT_SECRET_DIR);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(CREDENTIALS_FOLDER)))
.setAccessType("offline")
.build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Script service = new Script.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
Script.Projects projects = service.projects();
// Creates a new script project.
Project createOp = projects.create(new CreateProjectRequest().setTitle("My Script")).execute();
// Uploads two files to the project.
File file1 = new File()
.setName("hello")
.setType("SERVER_JS")
.setSource("function helloWorld() {\n console.log(\"Hello, world!\");\n}");
File file2 = new File()
.setName("appsscript")
.setType("JSON")
.setSource("{\"timeZone\":\"America/New_York\",\"exceptionLogging\":\"CLOUD\"}");
Content content = new Content().setFiles(Arrays.asList(file1, file2));
Content updatedContent = projects.updateContent(createOp.getScriptId(), content).execute();
// Logs the project URL.
System.out.printf("https://script.google.com/d/%s/edit\n", updatedContent.getScriptId());
}
}
如果有人知道发生了什么或如何解决此问题,请告诉我。我已经搜索了一段时间,所以我相信这是一个新主题。任何帮助表示赞赏!