如何通过身份验证的Google服务帐户调用youtube API在java中生成令牌

时间:2014-12-17 15:54:58

标签: java youtube

我正在开发用于获取特定youtube视频详细信息的Web应用程序。为此我使用google服务帐户执行了代码,例如调用授权过程。我认为身份验证已完成且无法生成令牌。我运行我的代码时出现错误比如“INVALID_GRANT”。请帮我解决这个问题。

        here my sample code is as:

    String clientId = "Something.apps.googleusercontent.com";
                @SuppressWarnings({ "unchecked", "rawtypes" })
                List<String>scops = new <String>ArrayList();
                scops.add("https://www.googleapis.com/auth/youtubepartner");
                final HttpTransport TRANSPORT = new NetHttpTransport();
                final JsonFactory JSON_FACTORY = new JacksonFactory();

                GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(clientId)
                .setServiceAccountScopes(scops)
                .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
                .build();
                System.out.println("credential............."+credential);
                YouTube youtube=new YouTube.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("test").build();
                System.out.println("Responce.............."+youtube);
                com.google.api.services.youtube.YouTube.Activities act=youtube.activities();
                com.google.api.services.youtube.YouTube.Activities.List list=act.list("");
                list.execute();
                System.out.println("list Details...................."+list);

    here i passes Youtube Api to retrieving the list.

    error :

    Exceptioncom.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
    {
      "error" : "invalid_grant"
    }

    i m waiting for all of ur response to resolve this problem..Thanks all once again to spend time with my pro

1 个答案:

答案 0 :(得分:0)

我对您的身份验证方法并不熟悉,但有两件事:

  1. .setServiceAccountId(clientId)中您使用 clientId 作为参数,但在纪录片中声明它通常是电子邮件地址
  2.   

    Builder com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder.setServiceAccountId(String serviceAccountId)

         

    @Beta

         

    Beta

         

    设置服务帐户ID(通常是电子邮件地址)或null。

    1. .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))中指定了一个名为 key.p12 的文件,该文件不是文件
    2. 很抱歉,如果您上面的代码不是真正的代码,只是为了展示目的。


      如果这两件事没有帮助,你可以尝试这种方法(它对我有用):

              // FileDataStoreFactory is used to create new credentials and 
              // simultaneously loads previously generated credentials from a file
              // Specify a location where the file should be stored via DIRECTORY
              // The file is automatically generated and encrypted
              FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(DIRECTORY));
      
              // Create the GoogleAuthorizationCodeFlow. This is needed to get the user (you?)
              // approval (you need that so you can access data). getClientSecrets() returns a
              // GoogleClientSecrets. If you want to, I can provide some code :)
              // Here you also specify your scopes
              GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, getClientSecrets(), Arrays.asList(scope))
                  .setCredentialDataStore(StoredCredential.getDefaultDataStore(fileDataStoreFactory)).build();
      
              // With the GoogleAuthorizationCodeFlow you can create a Credential
              Credential cred = flow.loadCredential("user");
      
              // Check if the cred is null or expired (credentials last about an hour I think)
              if(cred == null || (cred.getExpiresInSeconds() < 100 && !cred.refreshToken())){
                  // If it is expired, you need to refresh it via UserAuthorization, again, if you
                  // want to I can provide an example code
                  GoogleTokenResponse resp = getUserAuthorization(flow);
                  if(resp == null){
                      return null;
                  }
                  cred = flow.createAndStoreCredential(resp, "user");
              }
      

      在您的代码中,您可以使用与GoogleCredential完全相同的凭据:

                  System.out.println("credential............."+ cred);
                  YouTube youtube=new YouTube.Builder(TRANSPORT, JSON_FACTORY, cred).setApplicationName("test").build();
                  System.out.println("Responce.............."+youtube);
                  com.google.api.services.youtube.YouTube.Activities act=youtube.activities();
                  com.google.api.services.youtube.YouTube.Activities.List list=act.list("");
                  list.execute();
                  System.out.println("list Details...................."+list);
      

      我希望这会有所帮助:)


      为此我遵循了本教程:Google's Tutorial

      对于服务器端应用程序:Google's Tutorial about server-side applications

      使用JavaScript的网络应用程序:Google's Tutorial about web-applications