2016-05-25 7 views
0

最後のyoutube apiで動画をアップロードしようとしていますが、このエラーが発生するたびに発生します。原因:com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission。UserRecoverableAuthException:Youtube API v3(アンドロイド)のNeedPermission

これは私のコードです:私は、認証トークンの有効期限が切れて考えると、あなたは再びグーグルで認証する必要があなたの助け

答えて

1

ため

AsyncTask<Void, Void, Void> youtubeUploadTask = new AsyncTask<Void, Void, Void>() { 
    @Override 
    protected Void doInBackground(Void... params) { 

     YouTube youtube; 

     /** 
     * Define a global variable that specifies the MIME type of the video 
     * being uploaded. 
     */ 
     String VIDEO_FILE_FORMAT = "video/*"; 

     String SAMPLE_VIDEO_FILENAME = "sample-video.mp4"; 

     try { 
      youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, mActivity.credential).setApplicationName(
        mActivity.getResources().getString(R.string.app_name)).build(); 

      System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME); 

      // Add extra information to the video before uploading. 
      Video videoObjectDefiningMetadata = new Video(); 

      VideoStatus status = new VideoStatus(); 
      status.setPrivacyStatus("public"); 
      videoObjectDefiningMetadata.setStatus(status); 

      // Most of the video's metadata is set on the VideoSnippet object. 
      VideoSnippet snippet = new VideoSnippet(); 

      Calendar cal = Calendar.getInstance(); 
      snippet.setTitle("Test Upload via Java on " + cal.getTime()); 
      snippet.setDescription(
        "Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime()); 

      // Set the keyword tags that you want to associate with the video. 
      List<String> tags = new ArrayList<>(); 
      tags.add("test"); 
      tags.add("example"); 
      tags.add("java"); 
      tags.add("YouTube Data API V3"); 
      tags.add("erase me"); 
      snippet.setTags(tags); 

      // Add the completed snippet object to the video resource. 
      videoObjectDefiningMetadata.setSnippet(snippet); 

      File mediaFile = new File(filePath.toString()); 
      if (mediaFile.isFile()) { 
       InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, 
         new BufferedInputStream(new FileInputStream(mediaFile))); 

       YouTube.Videos.Insert videoInsert = youtube.videos() 
         .insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent); 

       // Set the upload type and add an event listener. 
       MediaHttpUploader uploader = videoInsert.getMediaHttpUploader(); 

       uploader.setDirectUploadEnabled(false); 

       MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() { 
        public void progressChanged(MediaHttpUploader uploader) throws IOException { 
         switch (uploader.getUploadState()) { 
          case INITIATION_STARTED: 
           System.out.println("Initiation Started"); 
           break; 
          case INITIATION_COMPLETE: 
           System.out.println("Initiation Completed"); 
           break; 
          case MEDIA_IN_PROGRESS: 
           System.out.println("Upload in progress"); 
           System.out.println("Upload percentage: " + uploader.getProgress()); 
           break; 
          case MEDIA_COMPLETE: 
           System.out.println("Upload Completed!"); 
           break; 
          case NOT_STARTED: 
           System.out.println("Upload Not Started!"); 
           break; 
         } 
        } 
       }; 
       uploader.setProgressListener(progressListener); 

       // Call the API and upload the video. 
       Video returnedVideo = videoInsert.execute(); 

       // Print data about the newly inserted video from the API response. 
       System.out.println("\n================== Returned Video ==================\n"); 
       System.out.println(" - Id: " + returnedVideo.getId()); 
       System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle()); 
       System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags()); 
       System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus()); 
       System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount()); 
      } 
     } 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(); 
     } 
     return null ; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 
}; 

感謝。 UserRecoverableAuthExceptionをキャッチして、をExceptionオブジェクトに呼び出して、認証Intentを取得します。次に、認証のためにActivityを再度開始します。

例コード -

try { 
    // Your upload code 
    ... 
} catch (UserRecoverableAuthIOException e) { 
    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); 
} 
+0

はあなたに例がありますか? –

+0

答えをサンプルコードで更新しました。 – jaibatrik

+0

ありがとう、これは解決策です!!!! –