Search code examples
androidandroid-youtube-apiyoutube-data-api

YouTube Data API v3 Authentification Error


I'm trying to access the YouTube Data API v3 from within my app to fetch some videos from a specific channnel. I created an API Key and inserted my package name and the SHA-1 fingerprint. However, it does not work.

This is the error:

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)

And here is the code, which sends the request:

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;
}

Any help is MUCH appreciated!


Solution

  • Fixed it by myself.

    I was just using the wrong API key.

    EDIT:

    Maybe I should mention that it worked when I used an browser api key instead of an android api key.