java Facebook 在 Android 上的集成 fbconnect 断开的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13880622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:19:22  来源:igfitidea点击:

Facebook integration on Android fbconnect broken link

javaandroidauthenticationfbconnect

提问by anothershrubery

I am trying to implement Facebook integration into my Android app and it goes off and logs in to Facebook fine, but when it tries to pass the access token back to the app it just returns:

我正在尝试将 Facebook 集成到我的 Android 应用程序中,它关闭并正常登录 Facebook,但是当它尝试将访问令牌传递回应用程序时,它只会返回:

The webpage at fbconnect://success#access_token=[ACCESS TOKEN]might be temporarily down or it may have moved permanently to a new web address.

位于 fbconnect://success#access_token= [ACCESS TOKEN]的网页 可能暂时关闭,或者它可能已永久移至新网址。

Obviously where [ACCESS TOKEN]is a long sequence of chars.

显然哪里[ACCESS TOKEN]是一长串字符。

I have got the App ID correct and added the key hash to facebook. But what process could I have missed?

我有正确的应用程序 ID 并将密钥哈希添加到 facebook。但是我可能错过了什么过程?

Code:

代码:

public class FacebookActivity extends Activity {

private static final String APP_ID = "[MY APP ID]";
private static final String[] PERMISSIONS = new String[] {"publish_stream"};

private static final String TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

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

    facebook = new Facebook();
    restoreCredentials(facebook);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_facebook);

    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    if (facebookMessage == null){
        facebookMessage = "Test wall post";
    }
    messageToPost = facebookMessage;

            if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, APP_ID, PERMISSIONS, new LoginDialogListener());
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
            parameters.putString("message", message);
            parameters.putString("description", "topic share");
            try {
                facebook.request("me");
        String response = facebook.request("me/feed", parameters, "POST");
        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("") ||
                response.equals("false")) {
            showToast("Blank response.");
        }
        else {
            showToast("Message posted to your facebook wall!");
        }
        finish();
    } catch (Exception e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
}

class LoginDialogListener implements DialogListener {
    @Override
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        finish();
    }
}

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

}

New to Android development so I'm sure it's something simple.

Android 开发新手,所以我相信这很简单。

回答by xiangxin

I updated the native facebook app on the device and everything works.

我更新了设备上的原生 facebook 应用程序,一切正常。

回答by s1m3n

Seems like the native Facebook App must be version 2 or higher. I am seeing the same issue for 1.9.6 version, and upgrading the Facebook app solves the issue.

似乎本机 Facebook 应用程序必须是版本 2 或更高版本。我在 1.9.6 版本中看到了同样的问题,升级 Facebook 应用程序解决了这个问题。