这就是我所做的。
首先是你的回调,
private Request.Callback requestCallback = new Request.Callback() {
@Override
public void onCompleted(Response response) {
if(response.getError() == null) {
Toast.makeText(YourActivity.this, "Posted to Facebook.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(YourActivity.this, "Post to Facebook failed.", Toast.LENGTH_SHORT).show();
}
}
};
然后,看看你是否已经拥有 publish_actions 权限,
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for(String string : subset) {
if(!superset.contains(string)) {
return false;
}
}
return true;
}
然后把这个方法放在你的 onClick() 方法中
public void uploadVideoToFb(String caption) {
// Get Facebook's active session.
Session session = Session.getActiveSession();
/*
* Check published permissions first.
*/
List<String> permissionList = session.getPermissions();
if(!isSubsetOf(FacebookFragment.PERMISSIONS, permissionList)) {
/*pendingPublishReauthorization = true;*/
/*
* Set additional permission requests to be able
* to publish on the Facebook feed.
* Inside PERMISSIONS is just "publish_actions".
*/
Session.NewPermissionsRequest newPermissionRequest = new Session.NewPermissionsRequest(this, FacebookFragment.PERMISSIONS);
session.requestNewPublishPermissions(newPermissionRequest);
return;
}
new TaskDownloadAndPostToFb(this, caption).execute();
}
我们的 publish_action 权限,
public static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
那么这是我的 AsyncTask,
@Override
public void onPostExecute(Integer result) {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + filename;
File videoFile = new File(baseDir);
if(videoFile.exists()) {
Session session = Session.getActiveSession();
if(session != null) {
try {
Request request = Request.newUploadVideoRequest(session, videoFileLocal, requestCallback);
/*
* Take note of where to get the reference
of your bundle, it should always be
request.getParameters()
*/
Bundle params = request.getParameters();
params.putString("description", this.caption);
request.setParameters(params);
request.executeAsync();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} else {
Log.i(TAG, "Video not found.");
}
}
当然,出于安全原因,我编辑了很多代码。让我知道是否缺少某些内容和损坏。
我正在使用Facebook's latest SDK 3.17 as of August 7, 2014