我有一個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);`
發表您的logcat –
com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden – Shaikh
你正在使用android鍵或瀏覽器鍵的哪個鍵? –