Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook Chat API: Authentication
    primarykey
    data
    text
    <p>just trying to get started on a little project of mine with the Facebook Chat API, but I'm already failing. This is mostly sample code from the Facebook dev site. </p> <p>Why is this not connecting? All I get is "An error occurred".</p> <pre><code>&lt;?php // Copyright 2004-present Facebook. All Rights Reserved. $STREAM_XML = '&lt;stream:stream '. 'xmlns:stream="http://etherx.jabber.org/streams" '. 'version="1.0" xmlns="jabber:client" to="chat.facebook.com" '. 'xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace"&gt;'; $AUTH_XML = '&lt;auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" '. 'mechanism="X-FACEBOOK-PLATFORM"&gt;&lt;/auth&gt;'; $CLOSE_XML = '&lt;/stream:stream&gt;'; $RESOURCE_XML = '&lt;iq type="set" id="3"&gt;'. '&lt;bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"&gt;'. '&lt;resource&gt;fb_xmpp_script&lt;/resource&gt;&lt;/bind&gt;&lt;/iq&gt;'; $SESSION_XML = '&lt;iq type="set" id="4" to="chat.facebook.com"&gt;'. '&lt;session xmlns="urn:ietf:params:xml:ns:xmpp-session"/&gt;&lt;/iq&gt;'; $START_TLS = '&lt;starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/&gt;'; function open_connection($server) { print "[INFO] Opening connection... "; $fp = fsockopen($server, 5222, $errno, $errstr); if (!$fp) { print "$errstr ($errno)&lt;br&gt;"; } else { print "connnection open&lt;br&gt;"; } return $fp; } function send_xml($fp, $xml) { fwrite($fp, $xml); } function recv_xml($fp, $size=4096) { $xml = fread($fp, $size); if ($xml === "") { return null; } // parses xml $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml, $val, $index); xml_parser_free($xml_parser); return array($val, $index); } function find_xmpp($fp, $tag, $value=null, &amp;$ret=null) { static $val = null, $index = null; do { if ($val === null &amp;&amp; $index === null) { list($val, $index) = recv_xml($fp); if ($val === null || $index === null) { return false; } } foreach ($index as $tag_key =&gt; $tag_array) { if ($tag_key === $tag) { if ($value === null) { if (isset($val[$tag_array[0]]['value'])) { $ret = $val[$tag_array[0]]['value']; } return true; } foreach ($tag_array as $i =&gt; $pos) { if ($val[$pos]['tag'] === $tag &amp;&amp; isset($val[$pos]['value']) &amp;&amp; $val[$pos]['value'] === $value) { $ret = $val[$pos]['value']; return true; } } } } $val = $index = null; } while (!feof($fp)); return false; } function xmpp_connect($options, $access_token) { global $STREAM_XML, $AUTH_XML, $RESOURCE_XML, $SESSION_XML, $CLOSE_XML, $START_TLS; $fp = open_connection($options['server']); if (!$fp) { return false; } // initiates auth process (using X-FACEBOOK_PLATFORM) send_xml($fp, $STREAM_XML); if (!find_xmpp($fp, 'STREAM:STREAM')) { return false; } if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) { return false; } // starting tls - MANDATORY TO USE OAUTH TOKEN!!!! send_xml($fp, $START_TLS); if (!find_xmpp($fp, 'PROCEED', null, $proceed)) { return false; } stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); send_xml($fp, $STREAM_XML); if (!find_xmpp($fp, 'STREAM:STREAM')) { return false; } if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) { return false; } // gets challenge from server and decode it send_xml($fp, $AUTH_XML); if (!find_xmpp($fp, 'CHALLENGE', null, $challenge)) { return false; } $challenge = base64_decode($challenge); $challenge = urldecode($challenge); parse_str($challenge, $challenge_array); // creates the response array $resp_array = array( 'method' =&gt; $challenge_array['method'], 'nonce' =&gt; $challenge_array['nonce'], 'access_token' =&gt; $access_token, 'api_key' =&gt; $options['app_id'], 'call_id' =&gt; 0, 'v' =&gt; '1.0', ); // creates signature $response = http_build_query($resp_array); // sends the response and waits for success $xml = '&lt;response xmlns="urn:ietf:params:xml:ns:xmpp-sasl"&gt;'. base64_encode($response).'&lt;/response&gt;'; send_xml($fp, $xml); if (!find_xmpp($fp, 'SUCCESS')) { return false; } // finishes auth process send_xml($fp, $STREAM_XML); if (!find_xmpp($fp,'STREAM:STREAM')) { return false; } if (!find_xmpp($fp, 'STREAM:FEATURES')) { return false; } send_xml($fp, $RESOURCE_XML); if (!find_xmpp($fp, 'JID')) { return false; } send_xml($fp, $SESSION_XML); if (!find_xmpp($fp, 'SESSION')) { return false; } // we made it! send_xml($fp, $CLOSE_XML); print ("Authentication complete&lt;br&gt;"); fclose($fp); return true; } //Gets access_token with xmpp_login permission function get_access_token($app_id, $app_secret, $my_url){ $code = $_REQUEST["code"]; if(empty($code)) { $dialog_url = "https://www.facebook.com/dialog/oauth?scope=xmpp_login". "&amp;client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($my_url) ; echo("&lt;script&gt;top.location.href='" . $dialog_url . "'&lt;/script&gt;"); } $token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;client_secret=" . $app_secret . "&amp;code=" . $code; $access_token = file_get_contents($token_url); parse_str($access_token, $output); return($output['access_token']); } function _main() { print "Test platform connect for XMPP&lt;br&gt;"; $app_id='APP ID'; $app_secret='APP SECRET'; $my_url = "URL OF THIS PHP FILE"; $uid = '1496822489'; $access_token = get_access_token($app_id,$app_secret,$my_url); print "access_token: ".$access_token."&lt;br&gt;"; $options = array( 'uid' =&gt; $uid, 'app_id' =&gt; $app_id, 'server' =&gt; 'chat.facebook.com', ); // prints options used print "server: ".$options['server']."&lt;br&gt;"; print "uid: ".$options['uid']."&lt;br&gt;"; print "app id: ".$options['app_id']."&lt;br&gt;"; if (xmpp_connect($options, $access_token)) { print "Done&lt;br&gt;"; } else { print "An error ocurred&lt;br&gt;"; } } // first test function goOnline() { $message = '&lt;presence /&gt;'; send_xml($fp, $message); } ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;test fb api&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php _main(); ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload