Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用服务器令牌连接到YouTube API_Java_Youtube Api_Youtube Data Api_Google Api Java Client - Fatal编程技术网

Java 使用服务器令牌连接到YouTube API

Java 使用服务器令牌连接到YouTube API,java,youtube-api,youtube-data-api,google-api-java-client,Java,Youtube Api,Youtube Data Api,Google Api Java Client,我正在成功使用以下代码连接到YouTube API: public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); public static final JsonFactory JSON_FACTORY = new JacksonFactory(); private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials"; publi

我正在成功使用以下代码连接到YouTube API:

public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {

    InputStream in = GoogleSheetImpl.class.getResourceAsStream("/client_secret_json");

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    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();
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
    return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}

private static YouTube youtube;

@Test
public void test() {

    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");

    try {
        Credential credential = authorize(scopes, "commentthreads");

        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("youtube-cmdline-playlistupdates-sample")
                .build();

        process();

    } catch (GoogleJsonResponseException e) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
public static final HttpTransport HTTP_TRANSPORT=new NetHttpTransport();
公共静态最终JsonFactory JSON_FACTORY=new JacksonFactory();
私有静态最终字符串凭据\u目录=“.oauth凭据”;
公共静态凭据授权(列表作用域、字符串credentialDatastore)引发IOException{
InputStream in=GoogleSheetImpl.class.getResourceAsStream(“/client\u secret\u json”);
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(JSON_工厂,新的InputStreamReader(in));
FileDataStoreFactory FileDataStoreFactory=newfiledatastorefactory(新文件(System.getProperty(“user.home”)+“/”+CREDENTIALS_目录));
DataStore DataStore=fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
HTTP_传输、JSON_工厂、clientSecrets、scopes)。setCredentialDataStore(数据存储)
.build();
LocalServerReceiver localReceiver=新的LocalServerReceiver.Builder().setPort(8080.build();
返回新的AuthorizationCodeInstalledApp(流,localReceiver)。授权(“用户”);
}
私有静态YouTube YouTube;
@试验
公开无效测试(){
列表范围=列表。newArrayList(“https://www.googleapis.com/auth/youtube.force-ssl");
试一试{
凭证=授权(作用域,“评论线程”);
youtube=new youtube.Builder(HTTP_传输、JSON_工厂、凭证)
.setApplicationName(“youtube cmdline播放列表更新示例”)
.build();
过程();
}捕获(GoogleJsonResponseException e){
System.err.println(“Throwable:+t.getMessage());
t、 printStackTrace();
}
}
但正如您所看到的,它使用OAuth来验证打开网页并要求我登录到我的Google帐户的身份


是否有某种方法可以使用服务器到服务器令牌?对于令牌,我不需要加载网页进行身份验证。有关于如何正确配置连接工厂的教程吗

并非所有谷歌API都支持服务帐户。YouTube api是不支持服务帐户身份验证的api之一


您需要使用Oauth2对用户进行身份验证,然后保存一个刷新令牌。然后您可以使用该刷新令牌请求一个新的访问令牌。

是否有一些方法可以在不打开网页和每次登录用户的情况下使用API?我需要一种方法从Linux服务器执行来自不同用户的多个调用。我没有打开网页的选项。是的,您可以按照我所说的进行操作,并在存储刷新令牌(使用代码将其上载到服务器)后对其进行身份验证,这样您就可以在需要时使用刷新令牌请求新的访问令牌。只要你有那个刷新令牌,你就不需要再次验证它。我发现了一个阻塞问题。我最多可以在谷歌登录页面中验证10个帐户。有什么方法可以进行更多身份验证吗?您使用的是服务帐户而不是google signin这对您没有影响。主要问题是->我无法使用YouTube Java Api的服务帐户,因为只支持通过web界面登录。我找不到通过刷新令牌使用登录的解决方案。