【问题标题】:Get youtube title fail获取 youtube 标题失败
【发布时间】:2019-08-30 01:30:33
【问题描述】:

我想获得 Youtube 标题和其他。

我遵循this question 中的描述。

我在这里做什么: 我使用改造来请求数据。

这是我的服务:

@GET("https://www.googleapis.com/youtube/v3/videos?key=" + C.googlDevApiKey +"&part=snippet,contentDetails,statistics,status")
    Call<JsonObject> getYoutubeInfo(
            @Query("video_id") String videoId
    );

它正在执行请求:

Api.getClient(context)
                .create(Service.class)
                .getYoutubeInfo("v=r2qd_KPQiZE&t=31s")
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        if (response.isSuccessful()){
                            Log.e("TAG", "youtube Response getYoutubeInfo: " + response.body().toString());
                        } else {
                            Log.e("TAG", "youtube Response getYoutubeInfo: error: " + response.code());
                        }

                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });

和日志拦截器:

2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: vary: Origin
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: vary: X-Origin
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: content-type: application/json; charset=UTF-8
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: date: Fri, 30 Aug 2019 00:32:23 GMT
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: expires: Fri, 30 Aug 2019 00:32:23 GMT
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: cache-control: private, max-age=0
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: x-content-type-options: nosniff
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: x-frame-options: SAMEORIGIN
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: x-xss-protection: 1; mode=block
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: server: GSE
2019-08-30 07:32:23.679 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: alt-svc: quic=":443"; ma=2592000; v="46,43,39"
2019-08-30 07:32:23.680 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: {
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:  "error": {
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:   "errors": [
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:    {
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:     "domain": "usageLimits",
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:     "reason": "ipRefererBlocked",
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:     "message": "The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.",
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:     "extendedHelp": "https://console.developers.google.com/apis/credentials?project=XXXXXXX"
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:    }
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:   ],
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:   "code": 403,
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:   "message": "The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions."
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp:  }
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: }
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: <-- END HTTP (639-byte body)
2019-08-30 07:32:23.681 1683-1760/id.lkmsbwm.bwmmobile.dev D/OkHttp: <-- 403 https://www.googleapis.com/youtube/v3/videos?key=AIzaSyCd71dtBE9gQhALXOOGo6krGT9F8A7IHJM&part=snippet,contentDetails,statistics,status&video_id=v%3Dr2qd_KPQiZE%26t%3D31s (256ms)

我在控制台中设置了限制使用您的 Android 应用程序。

如何解决

【问题讨论】:

    标签: android youtube-data-api


    【解决方案1】:

    根据您的日志,您的应用程序未在开发者控制台上注册,首先检查包名称

    follow Youtube doc and then call api

    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.VideoListResponse;
    
    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.Videos.List request = youtubeService.videos()
                .list("snippet,contentDetails,statistics");
            VideoListResponse response = request.setId("Ks-_Mh1QhMc").execute();
            System.out.println(response);
        }
    }
    

    【讨论】:

    【解决方案2】:

    我认为问题是您的 api 未从 google API 控制台启用。启用您的 Youtube v3 API。请尝试以下步骤。

    单击this link 并从左上角选择您的项目。 点击here获取参考图片。 现在单击 YouTube Data API v3 并启用 API。 它看起来像this

    对于您的代码,我猜您是在此处传递视频 ID。

    Api.getClient(context)
                .create(Service.class)
                .getYoutubeInfo("v=r2qd_KPQiZE&t=31s")
                .enqueue(new Callback<JsonObject>()
    

    所以您只需要传递 id 而无需任何其他参数,那么您的代码将如下所示

    Api.getClient(context)
                .create(Service.class)
                .getYoutubeInfo("r2qd_KPQiZE")
                .enqueue(new Callback<JsonObject>()
    

    【讨论】:

    • 我确定启用了 Youtube Data API v3,而不是更改没有前缀 v= 的视频 ID,仍然失败,并显示相同的错误消息
    • 您是否仔细检查了传递给查询的密钥。这里有你可以尝试的东西。 [1] 尝试将 OAuth2.0 密钥传递给您的查询,[2] 尝试传递您在启用 youtube v3 API 后获得的密钥。
    猜你喜欢
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2015-08-09
    • 1970-01-01
    • 2015-07-30
    • 2017-10-14
    • 2015-10-23
    相关资源
    最近更新 更多