gpt4 book ai didi

java - 尝试读取.json文件时发生NullPointerException-Youtube Java Api

转载 作者:行者123 更新时间:2023-12-03 05:23:12 25 4
gpt4 key购买 nike

我正在尝试使用youtube的API在Java上的youtube中上传视频,但出现此错误:

Throwable: null
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at youtube.Auth.authorize(Auth.java:55)
at youtube.UploadVideo.<init>(UploadVideo.java:61)
at lyricspro.Main.main(Main.java:21)

这是.json文件:
{
"installed": {
"client_id": "id1234",
"client_secret": "secret123456"
}
}

这是发生错误的类:
package youtube;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.StoredCredential;
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.http.HttpTransport;
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.DataStore;
import com.google.api.client.util.store.FileDataStoreFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
* Shared class used by every sample. Contains methods for authorizing a user and caching credentials.
*/
public class Auth {

/**
* Define a global instance of the HTTP transport.
*/
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

/**
* Define a global instance of the JSON factory.
*/
public static final JsonFactory JSON_FACTORY = new JacksonFactory();

/**
* This is the directory that will be used under the user's home directory where OAuth tokens will be stored.
*/
private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";

/**
* Authorizes the installed application to access user's protected data.
*
* @param scopes list of scopes needed to run youtube upload.
* @param credentialDatastore name of the credential datastore to cache OAuth tokens
*/
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {


// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

// Checks that the defaults have been replaced (Default = "Enter X here").
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential "
+ "into src/main/resources/client_secrets.json");
System.exit(1);
}

// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
.build();

// Build the local server and bind it to port 8080
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}
}

该行中发生错误:
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("client_secrets.json"));

我试图将.json文件放置在项目文件夹内的所有可能位置,但是我无法解决。有什么建议么 ?

最佳答案

而是使用以下代码:

FileInputStream fis=new FileInputStream(new File(".\\client_secret.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(fis));

关于java - 尝试读取.json文件时发生NullPointerException-Youtube Java Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045328/

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