Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How can I make a request to youtube API without oauth authentication?

$
0
0

I have this code below where I'm using the youtube API to get results for a search query. The problem is that every time I make a request I have to authorize it with oauth again, where it opens it in the browser and I have to log in. I'm not trying to access any user data, only search results from a query, so I don't see why the user would have to login. How do I get around this?

/** * Sample Java code for youtube.channels.list * See instructions for running these code samples locally: * https://developers.google.com/explorer-help/guides/code_samples#java */import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.security.GeneralSecurityException;import java.util.Arrays;import java.util.Collection;import com.google.api.client.auth.oauth2.Credential;import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;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.youtube.YouTube;import com.google.api.services.youtube.model.SearchListResponse;public class YoutubeAPI{    private YouTube youtube;    private static final String CLIENT_SECRETS = "/client_secret.json";    private static final Collection<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/youtube.readonly");    private static final String APPLICATION_NAME = "My Application";    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();    public YoutubeAPI()    {        try        {            youtube = getService();        } catch (GeneralSecurityException | IOException e)        {            e.printStackTrace();        }    }    /**     * Create an authorized Credential object.     *     * @return an authorized Credential object.     * @throws IOException     */    private Credential authorize(final NetHttpTransport httpTransport) throws IOException    {        // Load client secrets.        InputStream in = YoutubeAPI.class.getResourceAsStream(CLIENT_SECRETS);        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));        // Build flow and trigger user authorization request.        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES).build();        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");        return credential;    }    /**     * Build and return an authorized API client service.     *     * @return an authorized API client service     * @throws GeneralSecurityException, IOException     */    private YouTube getService() throws GeneralSecurityException, IOException    {        final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();        Credential credential = authorize(httpTransport);        return new YouTube.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();    }    public SearchListResponse search(String query, String type)    {        try        {            YouTube.Search.List request = youtube.search().list("snippet");            SearchListResponse response = request.setMaxResults(25L).setQ(query).setType(type).execute();            return response;        } catch (IOException e)        {            e.printStackTrace();            return null;        }    }}

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>