YouTube Data API v3身份验证错误

时间:2016-06-08 13:26:46

标签: android android-youtube-api youtube-data-api

我尝试从我的应用中访问YouTube Data API v3,以从特定的channnel中获取一些视频。我创建了一个API密钥并插入了我的包名和SHA-1指纹。但是,它不起作用。

这是错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
   "code" : 403,
   "errors" : [ {
       "domain" : "usageLimits",
       "message" : "The Android package name and signing-certificate fingerprint, null and null, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions.",
 "reason" : "ipRefererBlocked",
 "extendedHelp" : "https://console.developers.google.com/apis/credentials?project=1097633804344"
   } ],
       "message" : "The Android package name and signing-certificate fingerprint, null and null, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions."
 }
 at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
 at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
 at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
 at de.axelrindle.youtubeapitest.YoutubeConnector.fetchVideoIDS(YoutubeConnector.java:59)
 at de.axelrindle.youtubeapitest.util.VideoIDFetcher.fetchVideoIDS(VideoIDFetcher.java:62)
 at de.axelrindle.youtubeapitest.InitService.onHandleIntent(InitService.java:57)
 at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.os.HandlerThread.run(HandlerThread.java:61)

以下是发送请求的代码:

YoutubeConnector.class

public YoutubeConnector(Context context) {
    this.context = context;
    youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) throws IOException {}
    }).setApplicationName("YouTube Data API v3 Test").build();

    try {
        query = youTube.search().list("id,snippet");
        query.setKey(DevKey.YOUTUBE_API);
        query.setType("video");
        query.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url)");
        query.setChannelId("UC3ifTl5zKiCAhHIBQYcaTeg");
        query.setMaxResults(maxResults);
    } catch (IOException e) {
        Log.e("YoutubeConnector", "Failed to connect to YouTube: " + e.getMessage());
    }
}

public List<String> fetchVideoIDS() {

    List<String> ids = new ArrayList<>();
    maxResults += 10;

    try {
        SearchListResponse response = query.execute();
        List<SearchResult> results = response.getItems();

        for (SearchResult result : results) {
            ids.add(result.getId().getVideoId());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return ids;
}

任何帮助很多赞赏!

1 个答案:

答案 0 :(得分:1)

我自己修正了。

我刚刚使用了错误的API密钥。

修改

也许我应该提一下,当我使用浏览器api密钥而不是 android api密钥时,它有效。