1

I have downloaded the sample command line program for Youtube Analytics api and Youtube Data Api v3 and imported in eclipse. So below I have mentioned the code for my OAuth 2.0 implementation class:

 package com.google.api.services.samples.youtube.cmdline;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.StoredCredential;
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.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.client.util.store.DataStore;
import com.google.api.client.util.store.FileDataStoreFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

/**
 * Shared class used by every sample. Contains methods for authorizing a user and caching credentials.
 */
public class Auth {

    /**
     * Define a global instance of the HTTP transport.
     */
    public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

    /**
     * Define a global instance of the JSON factory.
     */
    public static final JsonFactory JSON_FACTORY = new JacksonFactory();

    /**
     * This is the directory that will be used under the user's home directory where OAuth tokens will be stored.
     */
    private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";

    /**
     * Authorizes the installed application to access user's protected data.
     *
     * @param scopes              list of scopes needed to run youtube upload.
     * @param credentialDatastore name of the credential datastore to cache OAuth tokens
     */
    public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {

        // Load client secrets.
        Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

        // Checks that the defaults have been replaced (Default = "Enter X here").
        if (clientSecrets.getDetails().getClientId().startsWith("Enter")
                || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
            System.out.println(
                    "Enter Client ID and Secret from https://code.google.com/apis/console/?api=youtube"
                            + "into src/main/resources/client_secrets.json");
            System.exit(1);
        }

        // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
        FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
        DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
                .build();

        // Build the local server and bind it to port 8080
        LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

        // Authorize.
        return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
    }
}

and here are the warnings and errors that i get:

Dec 05, 2014 3:35:45 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for everybody: C:\Users\admin\.oauth-credentials
Dec 05, 2014 3:35:45 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for owner: C:\Users\admin\.oauth-credentials
2014-12-05 15:35:45.592:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2014-12-05 15:35:45.592:INFO::jetty-6.1.26
2014-12-05 15:35:45.622:WARN::failed SocketConnector@localhost:8080: java.net.BindException: Address already in use: JVM_Bind
2014-12-05 15:35:45.622:WARN::failed Server@532760d8: java.net.BindException: Address already in use: JVM_Bind
2014-12-05 15:35:45.622:INFO::Stopped SocketConnector@localhost:8080
IOException: java.net.BindException: Address already in use: JVM_Bind
java.io.IOException: java.net.BindException: Address already in use: JVM_Bind
    at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:107)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76)
    at com.google.api.services.samples.youtube.cmdline.Auth.authorize(Auth.java:75)
    at com.google.api.services.samples.youtube.cmdline.analytics.YouTubeAnalyticsReports.main(YouTubeAnalyticsReports.java:71)
Caused by: java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at org.mortbay.jetty.bio.SocketConnector.newServerSocket(SocketConnector.java:80)
    at org.mortbay.jetty.bio.SocketConnector.open(SocketConnector.java:73)
    at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:283)
    at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.Server.doStart(Server.java:235)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:104)
    ... 3 more

i am stuck very badly... Any help regarding this will be appreciated...

Arsalan Gundroo
  • 145
  • 1
  • 11
  • 1
    Your service is already running. Kill the old instance, than it should work again. The needed info in your exception is this: "Address already in use:" – schlingel Dec 05 '14 at 15:40
  • I also try to get data from youtubeAnalytics API. But still now i couldn't get result. Can u post some sample with step need to run it. – Sakthivel Appavu Jan 06 '15 at 11:56
  • the following link has some samples https://github.com/youtube/api-samples – Arsalan Gundroo Jan 06 '15 at 18:44
  • @ArsalanGundroo, thanks for your response. I will try to run without using mvn. Its possible? i don't know. please help me. – Sakthivel Appavu Jan 07 '15 at 07:45
  • no you will have to import these sample projects as maven projects into your IDE and it is very simple. – Arsalan Gundroo Jan 07 '15 at 13:29
  • I imported this sample project into my workspace, But i get below error for this link - http://pastie.org/9821996 – Sakthivel Appavu Jan 09 '15 at 11:56
  • yeah even i had the same problem. Find that Auth.java class manually on that github link and paste it your project in a suitable package. then just change the import statement for Auth.java – Arsalan Gundroo Jan 09 '15 at 19:49
  • I got below response while run that sample on cmd line http://www.paste.org/76796, I need to done this urgently please help me with more details. – Sakthivel Appavu Jan 12 '15 at 07:05
  • i havent used cmd line for this purpose so i don't have a clue about it...extremely sorry :( – Arsalan Gundroo Jan 12 '15 at 09:00
  • @ArsalanGundroo Thanks, I run the "YouTubeAnalyticsReports.java" class before I did what you say on Auth.java, but again i get this error http://www.paste.org/76797. And also I followed this one https://github.com/youtube/api-samples/tree/master/java. Can u tell me step by step to run this project or if any other way. – Sakthivel Appavu Jan 12 '15 at 09:25
  • i used eclipse for runnin it...you can get help from this video: https://www.youtube.com/watch?v=pb_t5_ShQOM&feature=youtu.be – Arsalan Gundroo Jan 12 '15 at 09:47
  • I can watch this video later only, But this error only I get again and again, http://www.paste.org/76798 – Sakthivel Appavu Jan 12 '15 at 10:12
  • search the jar file or the maven dependency for that class (LocalServerReceiver or something) in maven central repository and use it in your code – Arsalan Gundroo Jan 12 '15 at 19:50

0 Answers0