问题
I am developing a chatbot for messenger platform using nodejs deployed on heroku. I am trying to get the user id in the webview. I have set the messenger extension field to true, whitelisted my domain, using latest updated version android app and also since now webview support the web browser, I am also viewing it in safari browser.
The messenger sdk is loading perfectly. I the check whether browser is supported or not which I get as result->true. Still I am facing problem getting the user id. My code is:
<script>
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.com/en_US/messenger.Extensions.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'Messenger'));
window.extAsyncInit = function () {
var isSupported = MessengerExtensions.isInExtension();
alert(isSupported);
// the Messenger Extensions JS SDK is done loading
MessengerExtensions.getUserID(function success(uids) {
var psid = uids.psid;
alert(psid);
}, function error(err) {
alert("Messenger Extension Error: " + err);
});
};
</script>
NOTE: I have searched the solution for this problem but none of them work, hence I am post this error.
回答1:
Well, I read the documentation once again. The documentation say that the webview for desktop works but presently doesn't support some caveats like getUserIds().
I found two alternate solution to over come the problem of getting the "psid", the first one is simple and you might be knowing about it as many chatbots like "2k17 Resolutions" are using it.
1.Adding the "senderid" in your nodejs app as parameter to the url of your webview and then fetching it on the page itself. The "senderid" and "psid" are same.
Getting the psid from the getContext() by calling it from messenger js sdk. The getContext() return 4 field in json object, which are "thread_type","tid","psid","signed_request" where psid is what I was looking for. Below is the complete working code.
<script> (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = "https://connect.facebook.com/en_US/messenger.Extensions.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'Messenger')); window.extAsyncInit = function () { var isSupported = MessengerExtensions.isInExtension(); alert(isSupported); // the Messenger Extensions JS SDK is done loading MessengerExtensions.getContext('YOU_APP_ID', function success(result){ alert("Success: "+result.psid); }, function error(result){ alert(JSON.stringify(result)); } ); }; </script>
And here is the link for Thread context documentation: https://developers.facebook.com/docs/messenger-platform/webview/context
EDIT: Two days back the messenger got few new updates out of which, one of the new update make the web view compatible with all browser and hence now getContext() is working with any browser.
来源:https://stackoverflow.com/questions/45895570/facebook-messenger-extension-error-2071010