使用youtube api从Android发布youtube视频时获得的权限不足

时间:2015-06-26 07:23:08

标签: android youtube comments

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

private void 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);`

1 个答案:

答案 0 :(得分:0)

似乎令牌存在一些问题,因为此错误仅在 -

时出现
{{#if requiredExp equals 0 and tile equals "SSE"}}
Expierince cannot be  equal to 0
{{/if}}

参考链接 -

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