Search code examples
androidfluttergoogle-apiyoutube-apiyoutube-data-api

Requests from this Android client application <empty> are blocked - Google APIs error Flutter


Problem description: I am using Youtube Data API to fetch some videos using the API key in a Flutter app. I have used this code to fetch my data:

    const FETCH_LINK =
        "$BASE_URL?part=$PART&playlistId=$PLAYLIST_ID&key=$API_KEY&maxResults=$MAX_RESULTS";
    var response = await http.get(Uri.parse(FETCH_LINK));

When I set the Applications restrictions to "None" in Credentials tab in GCP console of the project I'm getting my Data perfectly

Now when I try to restrict the data to only Android apps Its showing this error:

{error: {code: 403, message: Requests from this Android client application <empty> are blocked., errors: [{message: Requests from this Android client application <empty> are blocked., domain: global, reason: forbidden}], status: PERMISSION_DENIED, details: [{@type: type.googleapis.com/google.rpc.ErrorInfo, reason: API_KEY_ANDROID_APP_BLOCKED, domain: googleapis.com, metadata: {service: youtube.googleapis.com, consumer:

What I have already checked: I have added Debug and Release certificates and android package name correctly in the credentials tab.

What could be the possible error here? Is there anything I need to pass as headers in my request?

This is the endpoint I'm trying to fetch

https://youtube.googleapis.com/youtube/v3/playlistItems

My settings look like this: enter image description here


Solution

  • A answered here, we need to pass package name and SHA1 key name in headers. So my new request would look like this:

    const SHA1 = "0000000000000000000000000000000000000000";
    const PACKAGE_NAME = "com.yourapp.name";
    const HEADERS = {
          'Content-Type': 'application/json',
          'X-Android-Package': PACKAGE_NAME,
          'X-Android-Cert': SHA1b,
        };
    const FETCH_LINK =
            "$BASE_URL?part=$PART&playlistId=$PLAYLIST_ID&key=$API_KEY&maxResults=$MAX_RESULTS";
    var response = await http.get(Uri.parse(FETCH_LINK));