Youtube Data API v3 oAuth2无法更改权限

时间:2015-09-13 02:58:40

标签: youtube oauth-2.0 youtube-api authorization google-oauth

我正在使用google的apis来创建一个小型桌面应用程序,以便上传视频更易于管理(我管理网络中心频道,长篇大论,为什么我需要这个)。我已经按照谷歌的指南来获得一个起点,但使用oauth2的身份验证失败,并显示以下控制台输出:

    Sep 12, 2015 9:43:29 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for everybody: C:\Users\BossLetsPlays\.oauth-credentials
Sep 12, 2015 9:43:29 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNING: unable to change permissions for owner: C:\Users\BossLetsPlays\.oauth-credentials
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
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.bossletsplays.ytclient.utils.AuthUtil.authorize(AuthUtil.java:67)
    at com.bossletsplays.ytclient.utils.VideoUploader.upload(VideoUploader.java:48)
    at com.bossletsplays.ytclient.gui.MainGui$4.actionPerformed(MainGui.java:152)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
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)
    ... 40 more

我在很多地方看到过这个问题,但没有可行的解决方案。 Google表示通过抛出警告而不是IOExceptions来修复错误,但我仍然收到警告和IOExceptions。

以下是处理身份验证的代码:

public static final HttpTransport HTTP_TRANSPORT        = new NetHttpTransport();
public static final JsonFactory   JSON_FACTORY          = new JacksonFactory();
private static final String       CREDENTIALS_DIRECTORY = ".oauth-credentials";

public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {
    Reader clientSecretReader = new InputStreamReader(AuthUtil.class.getResourceAsStream("/client_secrets.json"));
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
    System.out.println(clientSecrets.getDetails().getClientId());
    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);
    }
    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();
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

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

是的,我已经替换了文件中的客户端ID和秘密。

1 个答案:

答案 0 :(得分:3)

问题出现在以下行中:

LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

可能是另一个进程使用的端口8080 ......

确保运行命令:

netstat -an | grep 8080

之后搜索进程并将其杀死