Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

android - Youtube API Key

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 signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.

In the MainActivity I have this code:

youtube = new YouTube.Builder(new NetHttpTransport(), JSON_FACTORY, new HttpRequestInitializer() {
                    @Override
                    public void initialize(HttpRequest httpRequest) throws IOException {

                    }
                }).setYouTubeRequestInitializer(new YouTubeRequestInitializer(apiKey)).setApplicationName("Some Name").build();

In the cloud console I have an ApiKey for Android, with the package name set and the SHA-1 number obtained with keytool command.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...