3

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

Apoorv pandey
  • 509
  • 8
  • 21

3 Answers3

2

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));
Apoorv pandey
  • 509
  • 8
  • 21
0

You have restricted your key that's why you are unable to send request through Android Please don't restrict your as you can see here below : enter image description here

The response I am receiving upon this

{
   "error":{
      "code":403,
      "message":"Requests from this iOS client application <empty> are blocked.",
      "errors":[
         {
            "message":"Requests from this iOS client application <empty> are blocked.",
            "domain":"global",
            "reason":"forbidden"
         }
      ],
      "status":"PERMISSION_DENIED",
      "details":[
         {
            "@type":"type.googleapis.com/google.rpc.ErrorInfo",
            "reason":"API_KEY_IOS_APP_BLOCKED",
            "domain":"googleapis.com",
            "metadata":{
               "service":"youtube.googleapis.com",
               "consumer":"projects/418596848743"
            }
         }
      ]
   }
}
Muhammad Ashir
  • 418
  • 2
  • 14
  • You may can restrict to Android by adding your package name along with SHA-1 Key or can go with a key that is not restricted to a specific platform. – Muhammad Ashir Nov 24 '21 at 16:23
  • No but I have set the API restriction to youtube data API, Selected APIs: YouTube Data API v3 – Apoorv pandey Nov 24 '21 at 16:23
  • Can you let me know about Application restrictions? – Muhammad Ashir Nov 24 '21 at 16:26
  • I have added my ss in question please check, also is it working for your device when you set the API restrictions to Youtube data API? – Apoorv pandey Nov 24 '21 at 16:28
  • yes, But you need to add your app properly with SHA-1 key Please generate it through methods given by Google Developer Console while restricting the key because that can cause the issue also otherwise try by setting it to none then it should work. – Muhammad Ashir Nov 24 '21 at 16:31
  • Yes, I got the keys from the commands given in right of the image I have uploaded in the question which are correct as I verified them on my device using third party apps too! – Apoorv pandey Nov 24 '21 at 16:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239556/discussion-between-apoorv-pandey-and-muhammad-ashir). – Apoorv pandey Nov 24 '21 at 16:56
0

Remove the ":" from the sha-1 key, the syntax of the support example is wrong