2

Using Android Studio, I am creating an app that lists YouTube videos based on some criteria. I am using the Search list method. In the app I make a call to the YouTube API and parse the results.

This app works fine using a 'Server' key when on my home Wifi. However, when switching to an 'Android' key, the app no longer works. The calls to the API are no longer returning the expected data, and are giving a java.io.FileNotFoundException: https://www.googleapis.com/youtube/v3/search... error.

I am setting up the YouTube API key at the Google Developers Console. I create a 'Public API access' key, using the SHA1 output from the command keytool -list -v -keystore .android/debug.keystore, and then appending a semicolon and package name 'com.mysite.projectname' (as is stated in the instructions).

I am having a hard time figuring out the issue. It seems that the API key is set up correctly and should be working (at least for development).

--Adding edit for answer below

I use the class below to store my API key. I access the key using DeveloperKey.DEVELOPER_KEY in other classes. As mentioned, this works when I have a 'Server' key instead of and 'Android' key.

package com.mysite.projectname;

public class DeveloperKey {
    public static final String DEVELOPER_KEY = "MyAPIKey";
}

Calling the API like:

https://www.googleapis.com/youtube/v3/search?part=snippet........&key=MyAPIKey
DJElbow
  • 3,345
  • 11
  • 41
  • 52

1 Answers1

1

I would post this as a comment but apparently I can't, so here goes nothing. How are you implementing the key in your app? The easiest way is to chuck it into strings.xml and call it using something like this:

        setContentView(R.layout.activity_youtube_player);
        //initialize
        YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
        youTubePlayerView.initialize(getString(R.string.api_youtube), this);

and strings.xml looking something like

<string name="api_youtube">MYFANCYKEYHERE</string>

Can you post snippets of your implementation?

EDIT

It sounds like you're grabbing the SHA1 key correctly (you can reference against this guy if you want to check if you're doing it right). To elaborate on my comment below, I'm not sure if you can actually grab youtube videos from that link from an Android platform. The Youtube API lets you do that very easily, though you have to remember to put in the appropriate permission <uses-permission android:name="android.permission.INTERNET" /> in your manifest first. I've given a link below that's a tutorial on how to do it the Google Approved (TM) way, and if you're bored and want to pull it the rtsp way, this fella has instructions how. (But I wouldn't recommend it because the quality's crap).

Community
  • 1
  • 1
Matter Cat
  • 1,538
  • 1
  • 14
  • 23
  • Mmph, I was asking more about how specifically you use the key in your youtube player. I've never used a server key before, but when I last got the FileNotFound error using an Android key, it was because I'd mixed up where to put the video id vs the api key. That obviously might not be your problem, but it's hard to tell what's going on without seeing how you're using the key. – Matter Cat Feb 02 '15 at 08:17
  • Well I am not having a problem with the YouTube Player, just parsing the JSON from a call to the Search list method. So I am making a request to `https://www.googleapis.com/youtube/v3/search?part=snippet........&key=MyAPIKey` and parsing the results. – DJElbow Feb 02 '15 at 08:24
  • Ahh. Okay. I've never done a direct youtube call from Android using the API, and to some extent I wonder if it's even possible. As far as I know, when it comes to using Youtube on Android, you've got two options. One (recommended!) you use the YoutubePlayer API, which is super easy to use, or two, you use the RTSP link, parse the JSON, and get a (usually really crappy quality) video. [This](http://javatechig.com/android/youtubeplayerview-example-in-android-using-youtube-api) is a decent example of how to implement the former. – Matter Cat Feb 02 '15 at 08:41