首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从使用YouTube数据API的个人项目中删除Google未经验证的警告

从使用YouTube数据API的个人项目中删除Google未经验证的警告
EN

Stack Overflow用户
提问于 2020-09-25 19:16:39
回答 1查看 501关注 0票数 0

我正在做一个不打算公开使用的个人项目。该项目使用YouTube数据API v3。当我运行代码时,我会看到这样的警告:

代码语言:javascript
运行
复制
> Task :ApiExample.main()
2020-09-25 14:52:25.740:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2020-09-25 14:52:25.748:INFO::jetty-6.1.26
2020-09-25 14:52:25.796:INFO::Started SocketConnector@localhost:*****
Please open the following address in your browser:

我不知道什么是本地主机,数字代表什么,所以我用星号替换它,以防它是私有的。

当我打开下面的链接,我会被提示登录到我的谷歌帐户,然后我会显示这个屏幕。

这个应用程序还没有被验证,这个应用程序还没有被Google验证。只有在了解并信任开发人员的情况下才能继续。如果您是开发人员,请提交一个验证请求以删除此屏幕。了解更多,谷歌还没有审查这个应用程序,也不能确认它是真实的。未经验证的应用程序可能对您的个人数据构成威胁。了解更多

我不想通过整个验证过程,因为这本身并不是一个真正的“应用程序”。我只是在玩API来学习它是如何工作的。有没有办法绕过验证过程,这样我就可以使用API来练习,而不必让Google批准我所做的随机项目?我不想每次使用这个程序时都要在线登录。

编辑

如果我正确理解了注释,那么每次运行程序时我都必须登录,因为我使用的是OAuth 2.0,而我只需要使用API,因为我的程序不需要访问我的特定帐户。这在授权凭证页面中得到了强烈的暗示,该页面指出:

此API支持两种类型的凭据。为您的项目创建合适的凭据: OAuth 2.0:每当应用程序请求私有用户数据时,它必须与请求一起发送OAuth 2.0令牌。您的应用程序首先发送客户端ID,可能还发送客户端机密以获取令牌。您可以为web应用程序、服务帐户或已安装的应用程序生成OAuth 2.0凭据。API :不提供OAuth 2.0令牌的请求必须发送一个API。该键标识您的项目并提供API访问、配额和报表。

当我第一次创建该项目时,我只打算使用API,而不打算使用OAuth 2.0凭据,因为它在页面上写了什么。但是,Java快速启动没有给出只使用API键的选项。相反,这里显示的演示代码看起来像

代码语言:javascript
运行
复制
/**
 * Sample Java code for youtube.channels.list
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#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.googleapis.json.GoogleJsonResponseException;
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.services.youtube.YouTube;
import com.google.api.services.youtube.model.ChannelListResponse;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collection;

public class ApiExample {
    private static final String CLIENT_SECRETS= "client_secret.json";
    private static final Collection<String> SCOPES =
        Arrays.asList("https://www.googleapis.com/auth/youtube.readonly");

    private static final String APPLICATION_NAME = "API code samples";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    /**
     * Create an authorized Credential object.
     *
     * @return an authorized Credential object.
     * @throws IOException
     */
    public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
        // Load client secrets.
        InputStream in = ApiExample.class.getResourceAsStream(CLIENT_SECRETS);
        GoogleClientSecrets clientSecrets =
          GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
            .build();
        Credential credential =
            new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
        return credential;
    }

    /**
     * Build and return an authorized API client service.
     *
     * @return an authorized API client service
     * @throws GeneralSecurityException, IOException
     */
    public static YouTube getService() throws GeneralSecurityException, IOException {
        final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        Credential credential = authorize(httpTransport);
        return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
    }

    /**
     * Call function to create API service object. Define and
     * execute API request. Print API response.
     *
     * @throws GeneralSecurityException, IOException, GoogleJsonResponseException
     */
    public static void main(String[] args)
        throws GeneralSecurityException, IOException, GoogleJsonResponseException {
        YouTube youtubeService = getService();
        // Define and execute the API request
        YouTube.Channels.List request = youtubeService.channels()
            .list("snippet,contentDetails,statistics");
        ChannelListResponse response = request.setId("UC_x5XG1OV2P6uZZ5FSM9Ttw").execute();
        System.out.println(response);
    }
}

在上面的代码示例中,client_secret.json是包含OAuth 2.0凭据的JSON文件。因此,既然已经说了这些,我相信我可以重声明我的问题如下:我如何使用一个API键而不是包含我的OAuth 2.0凭据的JSON来编写上面的代码示例?

编辑

我已经用以下方法替换了我的main方法:

代码语言:javascript
运行
复制
public static void main(String[] args)
        throws GeneralSecurityException, IOException, GoogleJsonResponseException {
    Properties properties = new Properties();
    try {
        InputStream in = ApiExample.class.getResourceAsStream("/" + "youtube.properties");
        properties.load(in);

    } catch (IOException e) {
        System.err.println("There was an error reading " + "youtube.properties" + ": " + e.getCause()
                + " : " + e.getMessage());
        System.exit(1);
    }
    YouTube youtubeService = getService();
    // Define and execute the API request
    YouTube.Channels.List request = youtubeService.channels()
            .list("snippet,contentDetails,statistics");
    String apiKey = properties.getProperty("youtube.apikey");
    request.setKey(apiKey);
    ChannelListResponse response = request.setId("UC_x5XG1OV2P6uZZ5FSM9Ttw").execute();
    System.out.println(response);

}

但是,每当我运行代码时,我仍然必须登录。

编辑

哇,我还在上面的代码示例中调用getService()方法。以下工作:

代码语言:javascript
运行
复制
YouTube youtubeService = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName(APPLICATION_NAME).build();

这个问题已经解决了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-27 14:04:17

如果您只打算使用Channels.list API端点来获取公共通道元数据,那么绝对没有必要使用OAuth 2.0授权(以及隐含的一次性身份验证)。

YouTube.Channels.list类有这样的方法,它允许您设置谷歌(通过其云控制台)提供的API键(作为私有数据):

setKey public YouTube.Channels.List setKey(java.lang.String key) 从类复制的YouTubeRequest描述: API密钥。API密钥标识项目,并为您提供API访问、配额和报表。除非您提供了OAuth 2.0令牌,否则需要。 重写:类YouTubeRequest<ChannelListResponse>中的 setKey

您可以查看来自Google的示例源文件GeolocationSearch.java,以查看setKey的运行情况:

代码语言:javascript
运行
复制
// Set your developer key from the {{ Google Cloud Console }} for
// non-authenticated requests. See:
// {{ https://cloud.google.com/console }}
String apiKey = properties.getProperty("youtube.apikey");
search.setKey(apiKey);

在您的例子中,上面的代码将完全按照相同的方式工作。只需将setKey应用于request (对象)变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64070166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档