gpt4 book ai didi

android - 使用 3.0 SDK 在 FB 墙上发帖

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:02 26 4
gpt4 key购买 nike

大家好程序员们,我在使用新的 Facebook SDK 时遇到了困难,

场景是:

我正在使用 fragment ,所以我按照以下步骤操作

Why doesn't Android Facebook interface work with Fragments?

现在我得到了 oncomplete 和 token ID

现在我想在我的墙上发帖,所以我创建了这个功能

private void publishStory(Session session) {

if (session != null){

// Check for publish permissions
session.addCallback(new StatusCallback() {

@Override
public void call(Session session, SessionState state,
Exception exception) {
List<String> PERMISSIONS = Arrays
.asList("publish_actions");
session
.requestNewPublishPermissions(new Session.NewPermissionsRequest(
getActivity(), PERMISSIONS));

Request request = Request.newStatusUpdateRequest(
session, "Temple Hello Word Sample",
new Request.Callback() {
@Override
public void onCompleted(Response response) {
Log.i("fb:done = ", response.getGraphObject() + ","
+ response.getError());
}
});
request.executeAsync();

}

});

并将声明放在 Oncomplete 上

    public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {



publishStory(session);
}
else
{
Log.v("FACEBOOK ", "NO ACCESS");
}
}
});
}

我的问题出在这行代码 session.addCallback(new StatusCallback() { 它跳过了下一步,从而结束了该功能而没有在我的墙上张贴。

我非常确定 session 处于 Activity 状态,因为我有 applicationID 和访问 token ..

有什么帮助吗?

谢谢

编辑

我根据下面的答案使用此代码并添加了新权限

private void publishStoryTEST(final Session session)
{

if (session != null)
{
Bundle postParams = new Bundle();
postParams.putString("message", "TEST");
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("JSON", "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
Log.e("post response", response.toString());
if (error != null)
{
}
else
{
}

}
};
List<String> PERMISSIONS = Arrays
.asList("publish_actions");
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(getActivity(), PERMISSIONS));
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);

RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

}
}

但我仍然收到错误提示

The user hasn't authorized the application to perform this action

为什么我仍然得到那个错误?权限从未调用过吗?

最佳答案

这个问题很常见,我正在与您分享我目前在我的应用程序中使用的发布方法的工作源代码。

private void publishStory(String status)
{
Session session = Session.getActiveSession();

if (session != null)
{
Bundle postParams = new Bundle();
postParams.putString("message", status);
Request.Callback callback = new Request.Callback()
{
public void onMalformedURLException(MalformedURLException e)
{

}
public void onIOException(IOException e)
{

}
public void onFileNotFoundException(FileNotFoundExceptione)
{

}
public void onFacebookError(FacebookError e)
{

}

public void onCompleted(Response response)
{
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try
{
postId = graphResponse.getString("id");
}
catch (JSONException e)
{
Log.i("JSON", "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
Log.e("post response", response.toString());
if (error != null)
{
}
else
{
}
}
};

Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}


我希望这能解决您的问题。

编辑:
请引用以下链接,了解使用适当的权限和发布功能设置 Facebook SDK 3.0 的详细过程。
Post to facebook after login fails Android
http://www.kpbird.com/2013/03/android-login-using-facebook-sdk-30.html

关于android - 使用 3.0 SDK 在 FB 墙上发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com