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
CodePudding user response:
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 :
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"
}
}
]
}
}
CodePudding user response:
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));