2015-05-21 149 views
2

這是我的整個代碼。我想給出一個來自YouTube的視頻ID以獲取與此視頻ID相關的評論。但總是表明我的權限不足。運行YouTube時,權限不足

我試圖解決這個問題,發現youtube .commentThreads().list("snippet").setVideoId("tLTm_POao1c") .setTextFormat("plainText").execute();是問題所在。具體而言,該行中的​​稱爲「權限不足」。 這是爲什麼?

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

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.util.List; 
import com.google.api.client.util.Preconditions;//aiyamaya 
import com.google.api.client.auth.oauth2.Credential; 
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 
import com.google.api.client.extensions.java6.auth.oauth2.FileCredentialStore; 
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.json.GoogleJsonResponseException; 
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.youtube.YouTube; 
import com.google.api.services.youtube.model.ResourceId; 
import com.google.api.services.youtube.model.Subscription; 
import com.google.api.services.youtube.model.SubscriptionSnippet; 
import com.google.common.collect.Lists; 
import com.google.api.services.youtube.model.CommentThreadListResponse; 
import com.google.api.services.youtube.model.CommentThread; 
import com.google.api.services.youtube.model.CommentSnippet; 
import com.google.api.services.youtube.YouTube; 
import com.google.api.services.samples.youtube.cmdline.youtube_cmdline_addsubscription_sample.Auth; 

//import com.google.api.services.samples.youtube.cmdline.Auth; 
/** 
* Demo of subscribing user to a channel using the YouTube Data API (V3) with 
* OAuth2 for authorization. 
* 
* @author Ibrahim Ulukaya 
*/ 
public class AddSubscription { 

    /** Global instance of the HTTP transport. */ 
    // private static final HttpTransport HTTP_TRANSPORT = new 
    // NetHttpTransport(); 

    /** Global instance of the JSON factory. */ 
    // private static final JsonFactory JSON_FACTORY = new JacksonFactory(); 

    /** Global instance of Youtube object to make all API requests. */ 
    private static YouTube youtube; 

    public static void main(String[] args) { 
     System.setProperty("proxySet", "true"); 
     System.setProperty("proxyHost", "bproxy.rmit.edu.au"); 
     System.setProperty("proxyPort", "8080"); 

     List<String> scopes = Lists 
       .newArrayList("https://www.googleapis.com/auth/youtube"); 

     try { 

      // Authorization. 
      Credential credential = Auth.authorize(scopes, "commentthreads"); 

      // This object is used to make YouTube Data API requests. 
      youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, 
        Auth.JSON_FACTORY, credential).setApplicationName(
        "youtube-cmdline-commentthreads-sample").build(); 
      // YouTube object used to make all API requests. 

      CommentThreadListResponse videoCommentsListResponse = youtube 
        .commentThreads().list("snippet").setVideoId("tLTm_POao1c") 
        .setTextFormat("plainText").execute(); 
      List<CommentThread> videoComments = videoCommentsListResponse 
        .getItems(); 

     } catch (GoogleJsonResponseException e) { 
      System.err.println("GoogleJsonResponseException code: " 
        + e.getDetails().getCode() + " : " 
        + e.getDetails().getMessage()); 
      e.printStackTrace(); 

     } catch (IOException e) { 
      System.err.println("IOException: " + e.getMessage()); 
      e.printStackTrace(); 
     } catch (Throwable t) { 
      System.err.println("Throwable: " + t.getMessage()); 
      t.printStackTrace(); 
     } 
    } 

    /* 
    * Returns a channel id (String) from user via the terminal. 
    */ 

} 

這是錯誤信息:

May 22, 2015 8:19:15 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly 
WARNING: unable to change permissions for everybody: C:\Users\E18300\.oauth-credentials 
May 22, 2015 8:19:15 AM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly 
WARNING: unable to change permissions for owner: C:\Users\E18300\.oauth-credentials 
2015-05-22 08:19:15.192:INFO::Logging to STDERR via org.mortbay.log.StdErrLog 
2015-05-22 08:19:15.193:INFO::jetty-6.1.26 
2015-05-22 08:19:15.212:INFO::Started [email protected]:8080 
2015-05-22 08:19:20.433:INFO::Stopped [email protected]:8080 
GoogleJsonResponseException code: 403 : Insufficient Permission 
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
{ 
    "code" : 403, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Insufficient Permission", 
    "reason" : "insufficientPermissions" 
    } ], 
    "message" : "Insufficient Permission" 
} 
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1045) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 
    at com.google.api.services.samples.youtube.cmdline.youtube_cmdline_addsubscription_sample.AddSubscription.main(AddSubscription.java:86) 

回答

0

隨着錯誤消息表明,你的請求不具有足夠的權限。如果你看一下API Reference Overview您將看到:

Every request must either specify an API key (with the key parameter) or provide an OAuth 2.0 token. Your API key is available in the API console's API Access pane for your project. 

比如我可以通過一個GET請求,此鏈接直接在瀏覽器來查看評論跟帖列表視頻: https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&key=YOUR_KEY&videoId=tLTm_POao1c。您需要將您的應用程序密鑰替換爲YOUR_KEY,您可以在Google developer console中找到該應用程序密鑰。

我不知道爲什麼註釋線程的代碼示例沒有提到需要包含API密鑰的任何內容(可能是因爲它假定您首先閱讀API概述)。但是 如果你看this other code sample,你會看到,您可以在本地文件開發者密鑰,你可以加載到一個Properties對象:

// 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); 
    } 

再往下行,API密鑰從提取Properties對象,並在搜索API調用用於:

 // Set your developer key from the Google Developers Console for 
     // non-authenticated requests. See: 
     // https://console.developers.google.com/ 
     String apiKey = properties.getProperty("youtube.apikey"); 
     search.setKey(apiKey); 
     search.setQ(queryTerm); 

以類似的方式,你可以在你的代碼中調用的setkey(),如JavaDoc中所述:https://developers.google.com/resources/api-libraries/documentation/youtube/v3/java/latest/com/google/api/services/youtube/YouTube.CommentThreads.List.html#setKey(java.lang.String)

所以,你可能需要添加somethi ng像這樣:

CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads() 
.list("snippet") 
.setKey(YOUR_KEY) 
.setVideoId("tLTm_POao1c") 
.setTextFormat("plainText") 
.execute(); 

甚至不需要屬性文件,除非您打算在編寫代碼並部署它之後更改API密鑰。

5

該部分缺少文檔,但您需要使用範圍https://www.googleapis.com/auth/youtube.force-ssl從YouTube API檢索評論。花了太多時間解決這個問題。