[英]Sharing a wall post by facebook android sdk
嗨,我是Android编程的新手。 我想在Facebook上分享一些描述的图像。 我已尝试在stackoverflow的答案中解释每个方法。 我的问题是facebook对话框打开但它没有指定的bundle参数。 请帮我解决一下这个。 我已经尝试了近20种不同的代码片段。 请给我完整的功能代码。
@Override
public void onClick(View v) {
fb = new Facebook(app_id);
Bundle par = new Bundle();
par.putString("name", "Ass");
fb.dialog(con,"feed",par, new DialogListener(){
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError arg0) {
// TODO Auto-generated method stub
}});
}
});
}
您是否已在开发页面上设置应用程序的Facebook端? 如果您打算使用他们的api和/或代表用户authtoken执行图表请求或挂帖,您需要在Facebook上注册您的应用程序。
您应该阅读本教程(查看第5部分,了解fb app注册)及其周围的所有相关信息,以便真正开始在您的应用中进行Facebook互动。 我知道我做了并最终创建了自己的库,用于登录快捷方式或图形请求等等......它不是那么简单,它也需要你一些时间。
也;
你在使用sdk的loginButton吗? 像这样:
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
它会自动处理用户登录facebook,无论他是否安装了facebook应用程序,并检索会话状态结果(onStatusChange回调)。 确保首先处理该权限并确保会话正确启动。
仍然,发布您的日志将使我们更好地了解您遇到的问题。
我不得不说,到目前为止,Android的facebook api非常可靠,所以你肯定会做错事。
<<<<<<编辑:>>>>>>
那么,假设您已经正确登录用户(session.getActiveSession()== Session.OPENED我相信),下一步是确保您启用了必要的权限。 这个例子来自官方的facebook文档,尝试它(在你的应用程序中执行publishStory()):
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
代码向用户供稿墙发出POST,并在包中指定图形参数。 如果会话没有所需的发布权限,则会发出权限请求。 如果用户授予了这些权限,则下次调用方法时应执行RequestAsyncTask。
正如您所看到的,基本的想法是让用户使用他的Facebook帐户登录(第一步),然后请求操作的必要权限,在这种情况下,发布墙贴的权限(第二步),最后使用墙贴所需的参数向“me / feed”发出图形请求。
在任何情况下,如果您仍然遇到问题,请尝试从您的日志进行调试,因为它表明由于会话无效或没有权限等请求失败...
在这种情况下将您的日志发布到此处,但这应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.