Youtube API Key

前端 未结 3 1185
太阳男子
太阳男子 2020-12-05 12:36

When I try to use YouTube API for Search, I get this error:

There was a service error: 403 : The request did not specify any Android package name or

相关标签:
3条回答
  • 2020-12-05 12:53

    At last I found a solution for this problem :)

    After creating API_KEY in Google Developer Console and restrict it with "Package name" and "SHA-1 certificate fingerprint", You have to provide these data in every youtube api request. Below the steps:

    1- get Package Name:

    String packageName = context.getPackageName();
    

    2- get SHA-1:

    private String getSHA1(String packageName){
        try {
            Signature[] signatures = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures;
            for (Signature signature: signatures) {
                MessageDigest md;
                md = MessageDigest.getInstance("SHA-1");
                md.update(signature.toByteArray());
                return BaseEncoding.base16().encode(md.digest());
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    3- Prepare youtube api http header:

    youTube = new YouTube.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) throws IOException {
            String packageName = context.getPackageName();
            String SHA1 = getSHA1(packageName);
    
            request.getHeaders().set("X-Android-Package", packageName);
            request.getHeaders().set("X-Android-Cert",SHA1);
        }
    }).setApplicationName(appName).build();
    

    4- Build your youtube api query as you like: For example to search for video:

    YouTube.Search.List query;
    query = youTube.search().list("id, snippet");
    query.setKey(YOUR_API_KEY);
    query.setType("video");
    query.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url)");
    query.setQ(search keywords);
    SearchListResponse response = query.execute();
    List<SearchResult> results = response.getItems();
    

    then process returned search results.

    0 讨论(0)
  • 2020-12-05 13:09

    Try to double check if you follow properly the setup when creating OAuth Credentials. And make sure you enable the YouTube Data API in your Developer Console.

    Here the steps that you need to do.

    1. In the Package name field, enter your Android app's package name

    2. In a terminal, run the Keytool utility to get the SHA1 fingerprint for your digitally signed .apk file's public certificate.

    keytool -exportcert -alias androiddebugkey -keystore path-to-debug-or-production-keystore -list -v

    1. Paste the SHA1 fingerprint into the form where requested.

    I also found here in this SO question answered by a Googler that a user has to go through OAuth2. Because Service accounts are not supported in Data API v3.

    0 讨论(0)
  • 2020-12-05 13:11

    After lot of trial and error, the thing which finally worked for me was changing API KEY restriction to None instead of Android from API Manager console and just save.

    After doing the above step I am able to make search API call from my Android device using my API KEY.

    0 讨论(0)
提交回复
热议问题