设备/模拟器安装了 Facebook 应用时,向用户的 Facebook 墙发布内容无法正常工作。

4
我创建了一个活动,使用此实现(请参见已接受的答案)在用户的Facebook主页上发布状态更新。
如果模拟器/手机没有安装Facebook应用程序,则可以正常使用。
如果模拟器/手机安装了Facebook应用程序,则Facebook应用程序将加载登录屏幕,但尝试登录后,Facebook应用程序只是消失并将我带回到我的应用程序。
有没有人在安装Facebook应用程序时遇到过这种情况?
我的代码:
public class AchievementActivity extends Activity implements DialogListener, OnClickListener{


private Facebook facebook;
Button facebookPostButton;
String defaultFacebookPost;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.achievements);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_layout);

    View achievementDivider = (View)findViewById(R.id.achievementDivider);
    int[] colors = {0, 0xff00ffff, 0};
    achievementDivider.setBackgroundDrawable(new GradientDrawable(Orientation.RIGHT_LEFT, colors));

    //get the title of the achievement from the intent that started this activity from the activity StatisticsActivity
    String achievementTitleString = getIntent().getStringExtra("title");

    String achievementTextToDisplay = getAchievementTextToDisplay(achievementTitleString);

    defaultFacebookPost = getDefaultPost(achievementTitleString);

    //ImageView achievementActivityAchievementBadgeImageView = (ImageView)findViewById(R.id.achievementActivityAchievementBadgeImageView);
    TextView achievementActivityBadgeTitleTextView = (TextView)findViewById(R.id.achievementActivityBadgeTitleTextView);
        achievementActivityBadgeTitleTextView.setText(achievementTitleString);

    TextView achievementActivityAchievementText = (TextView)findViewById(R.id.achievementActivityAchievementText);
        achievementActivityAchievementText.setText(achievementTextToDisplay);

    facebookPostButton = (Button)findViewById(R.id.facebookPostButton);
    facebookPostButton.setOnClickListener(this);


}

@Override
public void onComplete(Bundle values) {

    if (values.isEmpty())
    {
        Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT);

        return;
    }

    if (!values.containsKey("post_id"))
    {
        try
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", defaultFacebookPost);// the message to post to the wall
            facebook.dialog(AchievementActivity.this, "stream.publish", parameters, this);// "stream.publish" is an API call
        }
        catch (Exception e)
        {
            // TODO: handle exception
                System.out.println(e.getMessage());
        }
    }

    try 
    {
        facebook.logout(getApplicationContext());
    } 
    catch (MalformedURLException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onFacebookError(FacebookError error) 
{
    Toast.makeText(AchievementActivity.this, "onFacebookError", Toast.LENGTH_LONG); 
}

@Override
public void onError(DialogError e) 
{
    Toast.makeText(AchievementActivity.this, "onError", Toast.LENGTH_LONG); 
}

@Override
public void onCancel() 
{
    Toast.makeText(AchievementActivity.this, "onCancel", Toast.LENGTH_LONG);
}

@Override
public void onClick(View v)
{
    if (v == facebookPostButton)
    {
        facebook = new Facebook("my_facebook_api");
        // replace APP_API_ID with your own
        facebook.authorize(this, new String[] {"publish_stream", "read_stream", "offline_access"}, this); 
    }
}

private String getDefaultPost(String defaultTitleString) 
{
    //do some stuff here to get a string to post to wall

    return defaultPost;
}

private String getAchievementTextToDisplay(String achievementTitleString) {
    String achievementTextToDisplay = "DEFAULT";

    //do some stuff here to get text to display in the activity
            //this has nothing to do with the facebook post...

    return achievementTextToDisplay;
}

}

Logcat告诉我:

05-11 13:03:34.076: INFO/ActivityManager(98): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) }
05-11 13:03:34.246: INFO/ActivityManager(98): Displayed activity com.facebook.katana/.ProxyAuth: 158 ms (total 158 ms)
05-11 13:03:35.166: DEBUG/dalvikvm(12390): GC_FOR_MALLOC freed 6729 objects / 418424 bytes in 44ms
05-11 13:03:35.166: DEBUG/webviewglue(12390): nativeDestroy view: 0x527e20
05-11 13:03:35.166: DEBUG/NativeCrypto(12390): Freeing OpenSSL session
编辑: 在过去一年中,我不得不在多台机器上安装Android开发平台,并且总是在创建新的开发环境后遇到Facebook的问题。我发现这个简单的答案可能是导致Facebook实现在一个设备上工作而在另一个设备上不工作的原因...

秘密的Facebook API密钥(你在developer.facebook.com上列出的那个)将因为每个版本的应用程序使用不同的证书打包而不同。例如,假设你有两台开发机器。由于这两台机器将使用不同的证书构建你的应用程序,你必须确保为每台机器生成一个Facebook API秘密密钥,并在developer.facebook.com上列出它们。


你是否安装了最新的FB应用程序更新?过去当我使用FB SDK时,曾经遇到一些麻烦,因为FB应用程序没有更新到最新版本(在市场应用程序中检查我的应用程序,确保FB应用程序没有待处理的更新)。 - Mathias Conradt
@Mathias Lin - 我正在使用与 Facebook SDK 一起提供的相同版本的 Facebook。从 gitHub 获取时,他们会给你一个已编译的应用程序副本。 - dell116
3个回答

3

我已经找到了一个解决方法,但目前来看它并不是最好的。

facebook.authorize(activity, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, 
                new LoginDialogListener());

这将强制你的应用程序不使用设备上安装的官方Facebook应用程序的SSO。但是必须有更好的解决方案,因为有一些应用程序使用官方Facebook应用程序的SSO。


1

这是因为当您登录Facebook帐户时,会在设备上创建您的登录会话。完成任务后,您必须从Facebook注销。


谢谢回复,但我似乎仍然无法让Facebook应用程序保持活动状态。它只是消失了。我已经编辑了我的问题,包括我的活动代码。 - dell116
请提供更详细的答案! - tobias

0
private static final String[] PERMISSIONS =
        new String[] {"publish_stream", "read_stream", "offline_access"};  




@Override
public void onClick(View v)
{
    if (v == facebookPostButton)
    {
        mAsyncRunner    = new AsyncFacebookRunner(mFacebook);
                                   mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener());
                        mFacebook.authorize(FacebookImplement.this, PERMISSIONS, new AuthorizeListener());
    }
}



public class AuthorizeListener extends BaseDialogListener {

        public void onComplete(Bundle values) {

           Bundle params = new Bundle();
           params.putString("message"," Message");
       params.putString("description", "Wall Posting Description");        
        mAsyncRunner.request("me/feed", params, "POST",
                new UploadListener());         

        }

}

public class UploadListener extends BaseRequestListener {

        public void onComplete(final String response) {
              mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener());
}

}

 public class LogoutListener extends BaseRequestListener {


        public void onComplete(final String response) {         



        }
    }

也许这段代码可以帮助你。如果有任何问题,请毫不犹豫地提问。


非常感谢您提供代码,但我仍然得到相同的结果。Facebook应用程序打开,但立即关闭并将我带回我的应用程序的主要活动。此外,我建立了一个新项目,并在我的新项目中使用了来自www.facebook/developers的Facebook代码,一旦设备安装了Facebook,Facebook应用程序就会立即打开并关闭。当使用Webview时,一切正常。我完全不知所措。 - dell116
你让我找到正确的方向,而你最初提供的答案——没有正确地登录和退出会话是正确的。在看了你上面的代码并查看github上的示例后,我终于成功让它工作了。 - dell116
我有类似的问题。我不太明白你关于正确记录会话登录和退出的评论... 你能详细说明一下吗? - Mick Byrne
当您登录 Facebook 帐户时,设备将为该用户 ID 创建会话。当用户再次访问时,该用户 ID 将被用作登录,直到您注销 Facebook 帐户。 - Amit Thaper
我不太明白。Facebook API 的单点登录功能的重点在于应用程序用户每次使用应用程序时都不必看到登录屏幕。话虽如此,我发现他们的 Android API 非常不稳定。 - Jarrod Smith
我有同样的问题。但是我不能像你一样让它工作。你能给我提供更详细的解释吗? - tobias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接