2016-05-25 5 views
0

Ich versuche, ein Video mit dem letzten youtube API zu laden, aber die ganze Zeit bekomme ich diesen Fehler: Verursacht von: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission.UserRecoverableAuthException: NeedPermission in Youtube API v3 (Android)

Dies ist mein Code:

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

Danke für Ihre Hilfe

Antwort

1

Ich denke, die Auth-Token abgelaufen und Sie müssen erneut mit Google authentifizieren. Fangen Sie die UserRecoverableAuthException und rufen Sie getIntent auf das Objekt Exception, um die Authentifizierung Intent zu erhalten. Starten Sie dann erneut Activity zur Authentifizierung.

Beispielcode -

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

Sie haben ein Beispiel? –

+0

Nur die Antwort mit Beispielcode aktualisiert. – jaibatrik

+0

Danke das ist die Lösung !!!! –