“如何成功运行”;“Java快速入门”;谷歌驱动API示例?
我遵循谷歌的指南,试图开发一个搜索谷歌硬盘文件的功能。执行“如何成功运行”;“Java快速入门”;谷歌驱动API示例?,java,google-drive-api,google-oauth,google-api-java-client,Java,Google Drive Api,Google Oauth,Google Api Java Client,我遵循谷歌的指南,试图开发一个搜索谷歌硬盘文件的功能。执行gradle-q run或gradle run命令后,它打开一个网页并显示“Error:redirect\u uri\u mismatch”消息 错误消息: 代码如下: package main.java; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays
gradle-q run
或gradle run
命令后,它打开一个网页并显示“Error:redirect\u uri\u mismatch”消息
错误消息:
代码如下:
package main.java;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
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.HttpTransport;
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.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
public class Quickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Drive API Java Quickstart";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/drive-java-quickstart");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/**
* Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials at
* ~/.credentials/drive-java-quickstart
*/
private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");
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(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Drive client service.
*
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Drive service = getDriveService();
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
List<File> files = result.getFiles();
if (files == null || files.size() == 0) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
}
}
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Quickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.22.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.22.0'
compile 'com.google.apis:google-api-services-drive:v3-rev68-1.22.0'
}
我的谷歌开发者控制台:
要成功运行gradle-q run
或gradle run
命令,我应该做些什么?
如何正确配置“重定向URI”基于此,如果您使用(Android Studio中的推荐选项),您可以通过从项目根文件夹(其中包含gradlew
文件)中的命令行运行gradlew compileDebug--stacktrace来启用stacktrace。如果您没有使用gradle包装器,那么您可以使用gradle CompiledBug--stacktrace
(大概)。这样,您就可以知道错误的根源
您实际上不需要使用--stacktrace
运行,但是从命令行自行运行gradlew compileDebug
应该会告诉您错误在哪里
您也可以转到以下菜单项:Project>Clean…
如果这不起作用,请尝试从生成路径中删除jar,然后再次添加它们
其他:
如何正确配置“重定向URI”?
您可以检查此线程:
重定向URI是仅由正在执行oAuth2身份验证的web应用程序使用的对象;因此,当您创建一个新的客户端ID时,选择“web应用程序”作为ID类型,将有一个文本区域,您可以在其中输入所有允许的重定向URI(这些网页将由您编码,并且需要执行oauth2票证验证的功能)
如果您的应用程序不是web应用程序,则选择“已安装的应用程序”作为类型,您将获得一个可在Android/iOS/桌面应用程序中使用的密钥。但是,此密钥在web应用程序中根本不可用
如果您的web应用程序不需要写入任何数据或上载任何文件,则可以创建一个公共API密钥,该密钥仅作为参数包含在请求中
服务帐户(您在上图中显示)与YouTube API不兼容
嗨@abielita,我有一个进展并更新了问题。PTAL。你知道如何配置“重定向URI”吗?谢谢@Abielta。我找到了解决办法!!我应该选择“应用程序类型”而不是“其他”而不是“Web应用程序”。@Jack谢谢你的评论。我遇到了同样的错误,在将应用程序类型更改为“其他”后,它解决了问题。但我发现这里没有提到任何东西。顺便说一句,谢谢!:)