Messenger 扩展在 webview 中不工作

Messenger extension in not working in webview

将我的域列入白名单后,我尝试访问 Messenger 扩展程序以获取用户 ID,失败并出现错误:

Messenger Extensions are not enabled - could be "messenger_extensions" was not set on a url, the domain was not whitelisted or this is an outdated version of Messenger client

我在 google chrome 上尝试了 Messenger,但 firefox 出现了同样的错误。 messenger_extensions 设置为 true 且域已列入白名单;我确认过了。

为什么会带来那个消息?

您是否尝试通过浏览器访问它?如果是这样,那可能就是您遇到问题的原因。

尝试通过 phone 上的 Messenger 应用访问 url。这将是网页中的 iFrame,您将可以访问 MessengerExtensions sdk。

但不确定您应该如何在 Messenger 中登录。我做了这样的事情来测试它

window.extAsyncInit = function() {
// the Messenger Extensions JS SDK is done loading
    MessengerExtensions.getUserID(function success(uids) {
        // User ID was successfully obtained. 
        var psid = uids.psid;
        console.log("psid", psid)
            $('.error').html(psid)

    }, function error(err, errorMessage) {      
        // Error handling code
        console.log(err, errorMessage)
        $('.error').html(errorMessage)
    });    
};

您应该在消息中添加 image_url。此字段不是必需的,但如果您不包含它,则共享消息将仅在移动环境中有效。

示例:

var messageToShare = {
    attachment: {
        type: "template",
        payload: {
            template_type: "generic",
            elements: [{
                title: "Title",
                image_url: "https://image.flaticon.com/teams/slug/freepik.jpg",
                subtitle: 'A shared list from Tasks',
                default_action: {
                    type: "web_url",
                    url: "your_url",
                    messenger_extensions: true,
                    webview_height_ratio: "full",
                    fallback_url: "fallback_url"
                },
                buttons: [{
                    type: "web_url",
                    title: "title",
                    url: "your_url",
                    messenger_extensions: true,
                    webview_height_ratio: 'full',
                    fallback_url: "fallback_url"
                }]
            }]
        }
    }
};

此外,对于 messenger_extensions: true,您需要添加 fallback_url: "<your_fallback_url_here>" 才能使其正常工作。