Android 如何使用api密钥在youtube上上传视频?
我正在使用上传视频。 我有来自的API密钥,并且启用了YouTube数据API。所有设备上的应用程序在单通道中上载(否)。无法理解错误在哪里。服务器不以logcat(json格式)发送错误 代码:Android 如何使用api密钥在youtube上上传视频?,android,video,upload,key,android-youtube-api,Android,Video,Upload,Key,Android Youtube Api,我正在使用上传视频。 我有来自的API密钥,并且启用了YouTube数据API。所有设备上的应用程序在单通道中上载(否)。无法理解错误在哪里。服务器不以logcat(json格式)发送错误 代码: public class UploadToYoutubeTask extends ATask { private static final String TAG = UploadToYoutubeTask.class.getName(); /** * Change vide
public class UploadToYoutubeTask extends ATask {
private static final String TAG = UploadToYoutubeTask.class.getName();
/**
* Change video privacy settings.
* Variants: public/private/unlisted
* https://support.google.com/youtube/answer/157177?co=GENIE.Platform%3DDesktop&hl=en-GB
*/
public static final String PRIVACY_STATUS = "unlisted";
public static final String PARTS = "snippet,status,contentDetails";
/**
* MIME type
*/
private static String VIDEO_FILE_FORMAT = "video/*";
public static final String DEFAULT_KEYWORD = "ytdl";
public static final String[] SCOPES = {YouTubeScopes.YOUTUBE_UPLOAD};
private static final String API_KEY = "AIzaSyC8S8sn4RW4mH07SfX..";
File file;
public UploadToYoutubeTask(File file) {
super(TYPE);
this.file = file;
this.file = new File("/sdcard/..../20170826_101501_-110474017.mp4");
}
@Override
public int onBackgroundWork() {
Log.d(TAG, "onBackgroundWork: " + this);
HttpTransport transport = AndroidHttp.newCompatibleTransport();
// JsonFactory jsonFactory = new AndroidJsonFactory(); // GsonFactory
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
App.get().getApplicationContext(),
Arrays.asList(SCOPES));
// credential.setSelectedAccountName("blablaname@gmail.com");
credential.setSelectedAccount(new Account(
"blablaname@gmail.com",
App.get().getPackageName()));
credential.setBackOff(new ExponentialBackOff());
HttpRequestInitializer initializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
request.setLoggingEnabled(true);
// request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
}
};
YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, initializer);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, credential);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, null);
youtubeBuilder.setApplicationName(App.get().getString(R.string.app_name));
youtubeBuilder.setYouTubeRequestInitializer(new YouTubeRequestInitializer(API_KEY));
YouTube youtube = youtubeBuilder.build();
String videoId = tryUpload(
youtube,
file,
"test title",
"test descrioption",
new String[]{DEFAULT_KEYWORD});
Log.d(TAG, "onBackgroundWork: videoId=" + videoId);
return getStatus();
}
public String tryUpload(YouTube youtube, File file, String title, String description, String[] tags) {
String videoId = null;
try {
Video videoObjectDefiningMetadata = new Video();
videoObjectDefiningMetadata.setStatus(new VideoStatus().setPrivacyStatus(PRIVACY_STATUS));
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle(title);
snippet.setDescription(description);
snippet.setTags(Arrays.asList(tags));
videoObjectDefiningMetadata.setSnippet(snippet);
YouTube.Videos.Insert videoInsert = youtube.videos().insert(
PARTS,
videoObjectDefiningMetadata,
getMediaContent(file))
.setKey(API_KEY);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
Log.d(TAG, "progressChanged: " + uploader.getUploadState());
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
break;
case INITIATION_COMPLETE:
break;
case MEDIA_IN_PROGRESS:
break;
case MEDIA_COMPLETE:
case NOT_STARTED:
Log.d(TAG, "progressChanged: upload_not_started");
break;
}
}
};
uploader.setProgressListener(progressListener);
Log.d(TAG, "Uploading..");
Video returnedVideo = videoInsert.execute();
Log.d(TAG, "Video upload completed");
videoId = returnedVideo.getId();
Log.d(TAG, String.format("videoId = [%s]", videoId));
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
Log.e(TAG, "GooglePlayServicesAvailabilityIOException", availabilityException);
} catch (UserRecoverableAuthIOException userRecoverableException) {
Log.i(TAG, String.format("UserRecoverableAuthIOException: %s",
userRecoverableException.getMessage()));
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
return videoId;
}
private AbstractInputStreamContent getMediaContent(File file) throws FileNotFoundException {
InputStreamContent mediaContent = new InputStreamContent(
VIDEO_FILE_FORMAT,
new BufferedInputStream(new FileInputStream(file)));
mediaContent.setLength(file.length());
return mediaContent;
}
}
UserRecoverableAuthIOException: null
UploadToYoutubeTask: onBackgroundWork: videoId=null
LogCat:
public class UploadToYoutubeTask extends ATask {
private static final String TAG = UploadToYoutubeTask.class.getName();
/**
* Change video privacy settings.
* Variants: public/private/unlisted
* https://support.google.com/youtube/answer/157177?co=GENIE.Platform%3DDesktop&hl=en-GB
*/
public static final String PRIVACY_STATUS = "unlisted";
public static final String PARTS = "snippet,status,contentDetails";
/**
* MIME type
*/
private static String VIDEO_FILE_FORMAT = "video/*";
public static final String DEFAULT_KEYWORD = "ytdl";
public static final String[] SCOPES = {YouTubeScopes.YOUTUBE_UPLOAD};
private static final String API_KEY = "AIzaSyC8S8sn4RW4mH07SfX..";
File file;
public UploadToYoutubeTask(File file) {
super(TYPE);
this.file = file;
this.file = new File("/sdcard/..../20170826_101501_-110474017.mp4");
}
@Override
public int onBackgroundWork() {
Log.d(TAG, "onBackgroundWork: " + this);
HttpTransport transport = AndroidHttp.newCompatibleTransport();
// JsonFactory jsonFactory = new AndroidJsonFactory(); // GsonFactory
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
App.get().getApplicationContext(),
Arrays.asList(SCOPES));
// credential.setSelectedAccountName("blablaname@gmail.com");
credential.setSelectedAccount(new Account(
"blablaname@gmail.com",
App.get().getPackageName()));
credential.setBackOff(new ExponentialBackOff());
HttpRequestInitializer initializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
request.setLoggingEnabled(true);
// request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
}
};
YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, initializer);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, credential);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, null);
youtubeBuilder.setApplicationName(App.get().getString(R.string.app_name));
youtubeBuilder.setYouTubeRequestInitializer(new YouTubeRequestInitializer(API_KEY));
YouTube youtube = youtubeBuilder.build();
String videoId = tryUpload(
youtube,
file,
"test title",
"test descrioption",
new String[]{DEFAULT_KEYWORD});
Log.d(TAG, "onBackgroundWork: videoId=" + videoId);
return getStatus();
}
public String tryUpload(YouTube youtube, File file, String title, String description, String[] tags) {
String videoId = null;
try {
Video videoObjectDefiningMetadata = new Video();
videoObjectDefiningMetadata.setStatus(new VideoStatus().setPrivacyStatus(PRIVACY_STATUS));
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle(title);
snippet.setDescription(description);
snippet.setTags(Arrays.asList(tags));
videoObjectDefiningMetadata.setSnippet(snippet);
YouTube.Videos.Insert videoInsert = youtube.videos().insert(
PARTS,
videoObjectDefiningMetadata,
getMediaContent(file))
.setKey(API_KEY);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
Log.d(TAG, "progressChanged: " + uploader.getUploadState());
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
break;
case INITIATION_COMPLETE:
break;
case MEDIA_IN_PROGRESS:
break;
case MEDIA_COMPLETE:
case NOT_STARTED:
Log.d(TAG, "progressChanged: upload_not_started");
break;
}
}
};
uploader.setProgressListener(progressListener);
Log.d(TAG, "Uploading..");
Video returnedVideo = videoInsert.execute();
Log.d(TAG, "Video upload completed");
videoId = returnedVideo.getId();
Log.d(TAG, String.format("videoId = [%s]", videoId));
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
Log.e(TAG, "GooglePlayServicesAvailabilityIOException", availabilityException);
} catch (UserRecoverableAuthIOException userRecoverableException) {
Log.i(TAG, String.format("UserRecoverableAuthIOException: %s",
userRecoverableException.getMessage()));
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
return videoId;
}
private AbstractInputStreamContent getMediaContent(File file) throws FileNotFoundException {
InputStreamContent mediaContent = new InputStreamContent(
VIDEO_FILE_FORMAT,
new BufferedInputStream(new FileInputStream(file)));
mediaContent.setLength(file.length());
return mediaContent;
}
}
UserRecoverableAuthIOException: null
UploadToYoutubeTask: onBackgroundWork: videoId=null
build.gradle:
public class UploadToYoutubeTask extends ATask {
private static final String TAG = UploadToYoutubeTask.class.getName();
/**
* Change video privacy settings.
* Variants: public/private/unlisted
* https://support.google.com/youtube/answer/157177?co=GENIE.Platform%3DDesktop&hl=en-GB
*/
public static final String PRIVACY_STATUS = "unlisted";
public static final String PARTS = "snippet,status,contentDetails";
/**
* MIME type
*/
private static String VIDEO_FILE_FORMAT = "video/*";
public static final String DEFAULT_KEYWORD = "ytdl";
public static final String[] SCOPES = {YouTubeScopes.YOUTUBE_UPLOAD};
private static final String API_KEY = "AIzaSyC8S8sn4RW4mH07SfX..";
File file;
public UploadToYoutubeTask(File file) {
super(TYPE);
this.file = file;
this.file = new File("/sdcard/..../20170826_101501_-110474017.mp4");
}
@Override
public int onBackgroundWork() {
Log.d(TAG, "onBackgroundWork: " + this);
HttpTransport transport = AndroidHttp.newCompatibleTransport();
// JsonFactory jsonFactory = new AndroidJsonFactory(); // GsonFactory
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
App.get().getApplicationContext(),
Arrays.asList(SCOPES));
// credential.setSelectedAccountName("blablaname@gmail.com");
credential.setSelectedAccount(new Account(
"blablaname@gmail.com",
App.get().getPackageName()));
credential.setBackOff(new ExponentialBackOff());
HttpRequestInitializer initializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
request.setLoggingEnabled(true);
// request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
}
};
YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, initializer);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, credential);
// YouTube.Builder youtubeBuilder = new YouTube.Builder(transport, jsonFactory, null);
youtubeBuilder.setApplicationName(App.get().getString(R.string.app_name));
youtubeBuilder.setYouTubeRequestInitializer(new YouTubeRequestInitializer(API_KEY));
YouTube youtube = youtubeBuilder.build();
String videoId = tryUpload(
youtube,
file,
"test title",
"test descrioption",
new String[]{DEFAULT_KEYWORD});
Log.d(TAG, "onBackgroundWork: videoId=" + videoId);
return getStatus();
}
public String tryUpload(YouTube youtube, File file, String title, String description, String[] tags) {
String videoId = null;
try {
Video videoObjectDefiningMetadata = new Video();
videoObjectDefiningMetadata.setStatus(new VideoStatus().setPrivacyStatus(PRIVACY_STATUS));
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle(title);
snippet.setDescription(description);
snippet.setTags(Arrays.asList(tags));
videoObjectDefiningMetadata.setSnippet(snippet);
YouTube.Videos.Insert videoInsert = youtube.videos().insert(
PARTS,
videoObjectDefiningMetadata,
getMediaContent(file))
.setKey(API_KEY);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
Log.d(TAG, "progressChanged: " + uploader.getUploadState());
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
break;
case INITIATION_COMPLETE:
break;
case MEDIA_IN_PROGRESS:
break;
case MEDIA_COMPLETE:
case NOT_STARTED:
Log.d(TAG, "progressChanged: upload_not_started");
break;
}
}
};
uploader.setProgressListener(progressListener);
Log.d(TAG, "Uploading..");
Video returnedVideo = videoInsert.execute();
Log.d(TAG, "Video upload completed");
videoId = returnedVideo.getId();
Log.d(TAG, String.format("videoId = [%s]", videoId));
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
Log.e(TAG, "GooglePlayServicesAvailabilityIOException", availabilityException);
} catch (UserRecoverableAuthIOException userRecoverableException) {
Log.i(TAG, String.format("UserRecoverableAuthIOException: %s",
userRecoverableException.getMessage()));
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
return videoId;
}
private AbstractInputStreamContent getMediaContent(File file) throws FileNotFoundException {
InputStreamContent mediaContent = new InputStreamContent(
VIDEO_FILE_FORMAT,
new BufferedInputStream(new FileInputStream(file)));
mediaContent.setLength(file.length());
return mediaContent;
}
}
UserRecoverableAuthIOException: null
UploadToYoutubeTask: onBackgroundWork: videoId=null
依赖关系{
编译“com.google.code.gson:gson:${rootProject.gsonVersion}”
编译('com.google.api客户端:谷歌api客户端android:1.22.0')
编译'com.google.api:GoogleAPI服务youtube:v3-rev183-1.22.0'
编译'com.google.android.gms:play services auth:11.0.4'
编译'com.google.firebase:firebase核心:9.0.2'
编译'com.google.firebase:firebase消息:9.0.2'}