Android - Youtube API V3 search not working

2020-07-18 09:45发布

问题:

I'm having tough time integrating youtube v3 API. I tried all the possible solutions given in Youtube API Key.

Here is my code:

YouTube.Search.List query; 
query = youTube.search().list("id, snippet");
query.setKey(YOUR_API_KEY); 
query.setMaxResults(5L); 
query.setChannelId(channelId); 
query.setOrder("date"); 
SearchListResponse response = query.execute(); 
List<SearchResult> results = response.getItems();

As per Ayman Al-Absi's answer, I also tried passing packageName and and generated SHA1 (ZFzdtB22bpkKGc1kSgi0qxUPSWk=).

request.getHeaders().set("X-Android-Package", packageName);
request.getHeaders().set("X-Android-Cert",signature);

But when I did that I got error :

{ "code": 403, "errors": [ { "domain": "usageLimits", "message": "The Android package name and signing-certificate fingerprint, com.example.somename and ZFzdtB22bpkKGc1kSgi0qxUPSWk=, 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=1234567" } ], "message": "The Android package name and signing-certificate fingerprint, com.example.somename and ZFzdtB22bpkKGc1kSgi0qxUPSWk=, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions." }

API key configurations:

I have added SHA-1 fingerprints for both debug and prod environments.

回答1:

Ok this is old but I figured this out for my case and I thought it might help others. I went to oauth and it seemed to resolve.

the real issue is that if you use an unrestricted key [and maybe, depepending on the api enabled] have a billing account linked; the api should work. If it works unrestricted, you are on the right track.

Once you restrict it to android it will fail again with that key until you sign your app. The easiest way i found was the use a signing config for the variants under android in the gradle file.

signingConfigs {
    debug {
        storeFile file("/users/xxxxxxxxxx/Android/keystores/google_demos/debugandroid.jks")
        storePassword "xxxxxxxxxx"
        keyAlias "com.example.xxxxxxxxxx"
        keyPassword "xxxxxxxx"
    }
}