2015-06-26 79 views
0

我有一个Android应用程序,需要评论从我的应用程序发布到YouTube视频。我使用AccountManager来获取登录用户和访问令牌,但是当我使用访问令牌或api键发布评论时,我得到的答复是“权限不足”。代码如下。权限不足,同时发布Android使用YouTube的API视频的YouTube视频

私人无效insertVideoComments(){

new AsyncTask<Void, Void, Void>() { 

     @Override 
     protected Void doInBackground(Void... params) { 
      final HttpTransport transport = AndroidHttp 
        .newCompatibleTransport(); 
      final JsonFactory jsonFactory = new GsonFactory(); 

      AccountManager.get(getApplicationContext()).getAuthTokenByFeatures("com.google", "oauth2:https://gdata.youtube.com", null, PlayYoutubeVideosActivity.this, 
        null, null, new AccountManagerCallback<Bundle>() { 

         @Override 
         public void run(AccountManagerFuture<Bundle> future) { 
          Callback"); 
          try { 
           Bundle bundle = future.getResult(); 
           acc_name = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); 
           token = bundle.getString(AccountManager.KEY_AUTHTOKEN); 

           Log.d("test", "name: " + acc_name + "; token: " + token); 

          } catch (Exception e) { 
           Log.e("test", e.getClass().getSimpleName() + ": " + e.getMessage()); 
          } 
         } 
        }, null); 

      String url[] = {"https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/youtube.force-ssl"}; 
      List<String> scopes = Lists.newArrayList(url); 
      GoogleAccountCredential credential; 
      GoogleCredential CREDENTIAL = new GoogleCredential(); 

      credential = GoogleAccountCredential.usingOAuth2(PlayYoutubeVideosActivity.this, 
        Arrays.asList(url)); 

      credential.setSelectedAccountName(acc_name); 
      CREDENTIAL.setAccessToken(token); 

      youtube = new YouTube.Builder(
        transport, jsonFactory, credential).setApplicationName(
        ConstantsRT.APP_NAME).setHttpRequestInitializer(CREDENTIAL) 
        .setGoogleClientRequestInitializer(new YouTubeRequestInitializer(ConstantsRT.CLIENT_KEY)) 
        .build(); 

      CommentSnippet commentSnippet = new CommentSnippet(); 
      commentSnippet.setTextOriginal("This is for the news"); 

      //creating top level comment 
      Comment topLevelComment = new Comment(); 
      topLevelComment.setSnippet(commentSnippet); 
      // Insert video comment 

      CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet(); 
      commentThreadSnippet.setVideoId(videoPath); 
      commentThreadSnippet.setTopLevelComment(topLevelComment); 

      CommentThread commentThread = new CommentThread(); 
      commentThread.setSnippet(commentThreadSnippet); 

      try { 
       CommentThread videoCommentInsertResponse = youtube.commentThreads() 
         .insert("snippet", commentThread).setKey(ConstantsRT.API_KEY).execute(); 
       // Print information from the API response. 
       System.out 
         .println("\n================== Created Video Comment ==================\n"); 
       CommentSnippet snippet = videoCommentInsertResponse.getSnippet().getTopLevelComment() 
         .getSnippet(); 
       Log.d("test"," - Author: " + snippet.getAuthorDisplayName()); 
       Log.d("test"," - Comment: " + snippet.getTextDisplay()); 
       Log.d("test","\n-------------------------------------------------------------\n"); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    }.execute((Void[]) null);` 
+0

发表您的logcat –

+0

com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden – Shaikh

+0

你正在使用android键或浏览器键的哪个键? –

回答

0

似乎有一些问题与令牌作为这个错误来,只有当 -

The OAuth 2.0 token provided for the request specifies scopes that are insufficient for accessing the requested data. 

引用链接 -

​​
+0

那么,我该如何去做呢?我如何获得正确的代币? – Shaikh

+0

在这种情况下,客户端ID和客户端密钥都是必需的吗? – Shaikh