title: Java youtube API error 403 Daily Limit for Unauthenticated Use Exceeded
body: <p>i have an issue with the following code : i'm trying to use the youtube API v3 to run the simple geolocation code (first code) but i keep getting the service error 403 Daily Limit for Unauthenticated Use Exceeded Continued use requires signup.</p>
  
  <p>I have a valid key stored in the youtube.properties file and the youtube data API is activated on the google.console.developpers page. I don't understand the issue because when i run an even simpler code (2nd code) which uses the same authentication system, it works.</p>
  
  <p>first code : doesn't work</p>
  
  <pre><code> /*
   * Copyright (c) 2014 Google Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing permissions and limitations under
   * the License.
   */
  
  package com.google.api.services.samples.youtube.cmdline.data;
  
  import com.google.api.client.googleapis.json.GoogleJsonResponseException;
  import com.google.api.client.http.HttpRequest;
  import com.google.api.client.http.HttpRequestInitializer;
  import com.google.api.client.util.Joiner;
  import com.google.api.services.samples.youtube.cmdline.Auth;
  import com.google.api.services.youtube.YouTube;
  import com.google.api.services.youtube.model.GeoPoint;
  import com.google.api.services.youtube.model.SearchListResponse;
  import com.google.api.services.youtube.model.SearchResult;
  import com.google.api.services.youtube.model.Thumbnail;
  import com.google.api.services.youtube.model.Video;
  import com.google.api.services.youtube.model.VideoListResponse;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Properties;
  
  /**
   * This sample lists videos that are associated with a particular keyword and are in the radius of
   *   particular geographic coordinates by:
   *
   * 1. Searching videos with "youtube.search.list" method and setting "type", "q", "location" and
   *   "locationRadius" parameters.
   * 2. Retrieving location details for each video with "youtube.videos.list" method and setting
   *   "id" parameter to comma separated list of video IDs in search result.
   *
   * @author Ibrahim Ulukaya
   */
  public class GeolocationSearch {
  
      /**
       * Define a global variable that identifies the name of a file that
       * contains the developer's API key.
       */
      private static final String PROPERTIES_FILENAME = "youtube.properties";
  
      private static final long NUMBER_OF_VIDEOS_RETURNED = 25;
  
      /**
       * Define a global instance of a Youtube object, which will be used
       * to make YouTube Data API requests.
       */
      private static YouTube youtube;
  
      /**
       * Initialize a YouTube object to search for videos on YouTube. Then
       * display the name and thumbnail image of each video in the result set.
       *
       * @param args command line args.
       */
      public static void main(String[] args) {
          // Read the developer key from the properties file.
          Properties properties = new Properties();
          try {
              InputStream in = GeolocationSearch.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
              properties.load(in);
  
          } catch (IOException e) {
              System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
                      + " : " + e.getMessage());
              System.exit(1);
          }
  
          try {
              // This object is used to make YouTube Data API requests. The last
              // argument is required, but since we don't need anything
              // initialized when the HttpRequest is initialized, we override
              // the interface and provide a no-op function.
              youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
                  public void initialize(HttpRequest request) throws IOException {
                  }
              }).setApplicationName("youtube-cmdline-geolocationsearch-sample").build();
  
              // Prompt the user to enter a query term.
              String queryTerm = getInputQuery();
  
              // Prompt the user to enter location coordinates.
              String location = getInputLocation();
  
              // Prompt the user to enter a location radius.
              String locationRadius = getInputLocationRadius();
  
              // Define the API request for retrieving search results.
              YouTube.Search.List search = youtube.search().list("id,snippet");
  
              // Set your developer key from the {{ Google Cloud Console }} for
              // non-authenticated requests. See:
              // {{ https://cloud.google.com/console }}
              String apiKey = properties.getProperty("youtube.apikey");
              search.setKey(apiKey);
              search.setQ(queryTerm);
              search.setLocation(location);
              search.setLocationRadius(locationRadius);
  
              // Restrict the search results to only include videos. See:
              // https://developers.google.com/youtube/v3/docs/search/list#type
              search.setType("video");
  
              // As a best practice, only retrieve the fields that the
              // application uses.
              //search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
  
              search.setFields("items(id/videoId)");
              search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
  
              // Call the API and print results.
              SearchListResponse searchResponse = search.execute();
              List&lt;SearchResult&gt; searchResultList = searchResponse.getItems();
              List&lt;String&gt; videoIds = new ArrayList&lt;String&gt;();
  
              if (searchResultList != null) {
  
                  // Merge video IDs
                  for (SearchResult searchResult : searchResultList) {
                      videoIds.add(searchResult.getId().getVideoId());
                  }
                  Joiner stringJoiner = Joiner.on(',');
                  String videoId = stringJoiner.join(videoIds);
  
                  // Call the YouTube Data API's youtube.videos.list method to
                  // retrieve the resources that represent the specified videos.
                  YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet, recordingDetails").setId(videoId);
                  VideoListResponse listResponse = listVideosRequest.execute();
  
                  List&lt;Video&gt; videoList = listResponse.getItems();
  
                  if (videoList != null) {
                      prettyPrint(videoList.iterator(), queryTerm);
                  }
              }
          } catch (GoogleJsonResponseException e) {
              System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                      + e.getDetails().getMessage());
          } catch (IOException e) {
              System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
          } catch (Throwable t) {
              t.printStackTrace();
          }
      }
  
      /*
       * Prompt the user to enter a query term and return the user-specified term.
       */
      private static String getInputQuery() throws IOException {
  
          String inputQuery = "";
  
          System.out.print("Please enter a search term: ");
          BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
          inputQuery = bReader.readLine();
  
          if (inputQuery.length() &lt; 1) {
              // Use the string "YouTube Developers Live" as a default.
              inputQuery = "YouTube Developers Live";
          }
          return inputQuery;
      }
  
      /*
       * Prompt the user to enter location coordinates and return the user-specified coordinates.
       */
      private static String getInputLocation() throws IOException {
  
          String inputQuery = "";
  
          System.out.print("Please enter location coordinates (example: 37.42307,-122.08427): ");
          BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
          inputQuery = bReader.readLine();
  
          if (inputQuery.length() &lt; 1) {
              // Use the string "37.42307,-122.08427" as a default.
              inputQuery = "37.42307,-122.08427";
          }
          return inputQuery;
      }
  
      /*
       * Prompt the user to enter a location radius and return the user-specified radius.
       */
      private static String getInputLocationRadius() throws IOException {
  
          String inputQuery = "";
  
          System.out.print("Please enter a location radius (examples: 5km, 8mi):");
          BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
          inputQuery = bReader.readLine();
  
          if (inputQuery.length() &lt; 1) {
              // Use the string "5km" as a default.
              inputQuery = "5km";
          }
          return inputQuery;
      }
  
      /*
       * Prints out all results in the Iterator. For each result, print the
       * title, video ID, location, and thumbnail.
       *
       * @param iteratorVideoResults Iterator of Videos to print
       *
       * @param query Search query (String)
       */
      private static void prettyPrint(Iterator&lt;Video&gt; iteratorVideoResults, String query) {
  
          System.out.println("\n=============================================================");
          System.out.println(
                  "   First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
          System.out.println("=============================================================\n");
  
          if (!iteratorVideoResults.hasNext()) {
              System.out.println(" There aren't any results for your query.");
          }
  
          while (iteratorVideoResults.hasNext()) {
  
              Video singleVideo = iteratorVideoResults.next();
  
              Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();
              GeoPoint location = singleVideo.getRecordingDetails().getLocation();
  
              System.out.println(" Video Id" + singleVideo.getId());
              System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
              System.out.println(" Location: " + location.getLatitude() + ", " + location.getLongitude());
              System.out.println(" Thumbnail: " + thumbnail.getUrl());
              System.out.println("\n-------------------------------------------------------------\n");
          }
      }
  }*
  </code></pre>
  
  <p>2nd code, the one that works even though it uses the same authentication method:</p>
  
  <pre><code>/*
   * Copyright (c) 2012 Google Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing permissions and limitations under
   * the License.
   */
  
  package com.google.api.services.samples.youtube.cmdline.data;
  
  import com.google.api.client.googleapis.json.GoogleJsonResponseException;
  import com.google.api.client.http.HttpRequest;
  import com.google.api.client.http.HttpRequestInitializer;
  import com.google.api.client.http.HttpTransport;
  import com.google.api.client.http.javanet.NetHttpTransport;
  import com.google.api.client.json.JsonFactory;
  import com.google.api.client.json.jackson2.JacksonFactory;
  import com.google.api.services.samples.youtube.cmdline.Auth;
  import com.google.api.services.youtube.YouTube;
  import com.google.api.services.youtube.model.ResourceId;
  import com.google.api.services.youtube.model.SearchListResponse;
  import com.google.api.services.youtube.model.SearchResult;
  import com.google.api.services.youtube.model.Thumbnail;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Properties;
  
  /**
   * Print a list of videos matching a search term.
   *
   * @author Jeremy Walker
   */
  public class Search {
  
      /**
       * Define a global variable that identifies the name of a file that
       * contains the developer's API key.
       */
      private static final String PROPERTIES_FILENAME = "youtube.properties";
  
      private static final long NUMBER_OF_VIDEOS_RETURNED = 25;
  
      /**
       * Define a global instance of a Youtube object, which will be used
       * to make YouTube Data API requests.
       */
      private static YouTube youtube;
  
      /**
       * Initialize a YouTube object to search for videos on YouTube. Then
       * display the name and thumbnail image of each video in the result set.
       *
       * @param args command line args.
       */
      public static void main(String[] args) {
          // Read the developer key from the properties file.
          Properties properties = new Properties();
          try {
              InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
              properties.load(in);
  
          } catch (IOException e) {
              System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
                      + " : " + e.getMessage());
              System.exit(1);
          }
  
          try {
              // This object is used to make YouTube Data API requests. The last
              // argument is required, but since we don't need anything
              // initialized when the HttpRequest is initialized, we override
              // the interface and provide a no-op function.
              youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
                  public void initialize(HttpRequest request) throws IOException {
                  }
              }).setApplicationName("youtube-cmdline-search-sample").build();
  
              // Prompt the user to enter a query term.
              String queryTerm = getInputQuery();
  
              // Define the API request for retrieving search results.
              YouTube.Search.List search = youtube.search().list("id,snippet");
  
              // Set your developer key from the {{ Google Cloud Console }} for
              // non-authenticated requests. See:
              // {{ https://cloud.google.com/console }}
              String apiKey = properties.getProperty("youtube.apikey");
              search.setKey(apiKey);
              search.setQ(queryTerm);
  
              // Restrict the search results to only include videos. See:
              // https://developers.google.com/youtube/v3/docs/search/list#type
              search.setType("video");
  
              // To increase efficiency, only retrieve the fields that the
              // application uses.
              search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
              search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
  
              // Call the API and print results.
              SearchListResponse searchResponse = search.execute();
              List&lt;SearchResult&gt; searchResultList = searchResponse.getItems();
              if (searchResultList != null) {
                  prettyPrint(searchResultList.iterator(), queryTerm);
              }
          } catch (GoogleJsonResponseException e) {
              System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                      + e.getDetails().getMessage());
          } catch (IOException e) {
              System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
          } catch (Throwable t) {
              t.printStackTrace();
          }
      }
  
      /*
       * Prompt the user to enter a query term and return the user-specified term.
       */
      private static String getInputQuery() throws IOException {
  
          String inputQuery = "";
  
          System.out.print("Please enter a search term: ");
          BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
          inputQuery = bReader.readLine();
  
          if (inputQuery.length() &lt; 1) {
              // Use the string "YouTube Developers Live" as a default.
              inputQuery = "YouTube Developers Live";
          }
          return inputQuery;
      }
  
      /*
       * Prints out all results in the Iterator. For each result, print the
       * title, video ID, and thumbnail.
       *
       * @param iteratorSearchResults Iterator of SearchResults to print
       *
       * @param query Search query (String)
       */
      private static void prettyPrint(Iterator&lt;SearchResult&gt; iteratorSearchResults, String query) {
  
          System.out.println("\n=============================================================");
          System.out.println(
                  "   First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
          System.out.println("=============================================================\n");
  
          if (!iteratorSearchResults.hasNext()) {
              System.out.println(" There aren't any results for your query.");
          }
  
          while (iteratorSearchResults.hasNext()) {
  
              SearchResult singleVideo = iteratorSearchResults.next();
              ResourceId rId = singleVideo.getId();
  
              // Confirm that the result represents a video. Otherwise, the
              // item will not contain a video ID.
              if (rId.getKind().equals("youtube#video")) {
                  Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();
  
                  System.out.println(" Video Id" + rId.getVideoId());
                  System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
                  System.out.println(" Thumbnail: " + thumbnail.getUrl());
                  System.out.println("\n-------------------------------------------------------------\n");
              }
          }
      }
  }
  </code></pre>