测试帐号可以在本地开发中使用,没有任何权限限制,所有的功能模块都可以通过测试帐号实现,在功能开发完成后,部署到一台线上的测试环境进行facebook的审核,线上域名必须添加ssl加密
这里是我们申请成功的权限,新手第一次对接,感觉太南了,太南了。。。
授权登录和获取临时访问口令:
1 $appid = facebook appid; 2 $appcret = facebook appcret; 3 $callbackurl = facebook callbackurl; 4 $fb = new facebook\facebook([ 5 'app_id' => $appid, 6 'app_cret' => $appcret, 7 'default_graph_version' => 'v2.10', 8 ]); 9 10 $helper = $fb->getredirectloginhelper();11 try {12 $accesstoken = $helper->getaccesstoken();13 } catch(facebook\exceptions\facebookresponexception $e) {14 echo 'graph returned an error: ' . $e->getmessage();15 exit;16 } catch(facebook\exceptions\facebooksdkexception $e) {17 echo 'facebook sdk returned an error: ' . $e->getmessage();18 exit;19 }20 21 if (! ist($accesstoken)) {22 if ($helper->geterror()) {23 header('http/1.0 401 unauthorized');24 echo '请检查账号设置,facebook账号无法获得授权,详情:' . $helper->geterror() . ' ' . $helper->geterrorcode() . ' ' . $helper->geterrorreason() . ' ' . $helper->geterrordescription() . "\n";25 } el {26 header('http/1.0 400 bad request');27 echo 'bad request';28 }29 exit;30 }31 32 $accesstoken = $accesstoken->getvalue();33 $oauth2client = $fb->getoauth2client();34 $tokenmetadata = $oauth2client->debugtoken($accesstoken);35 $tokenmetadata->validateappid($appid);36 $tokenmetadata->validateexpiration();37 38 $appid = $tokenmetadata->getfield('app_id');39 $type = $tokenmetadata->getfield('type');40 $urid = $tokenmetadata-高情商的幽默对话>getfield('ur_id');41 $application = $tokenmetadata->getfield('application');42 $isvalid = $tokenmetadata->getfield('is_valid');43 $expiresat = $tokenmetadata->getfield('data_access_expires_at');44 $metadata = [45 "app_id" => $appid,46 "type"=> $type,47 "ur_id" => $urid,48 "application" => $application,49 "expires_at" => $expiresat,50 "is_valid" => $isvalid51 ];52 53 $auth = new facebook\authentication\accesstoken($accesstoken);54 if (! $auth->islonglived()) {55 try {56 $accesstoken = $oauth2client->getlonglivedaccesstoken($accesstoken);57 } catch (facebook\exceptions\facebooksdkexception $e) {58 echo "<p>error getting long-lived access token: " . $e->getmessage() . "</p>\n\n";59 exit;60 }61 }62 63 65 $this->cache->t($this->facebookaccesstokenkey,(string) $accesstoken );66 $this->cache->t($this->facebookkey, json_encode($metadata));67 68 header("location: {$callbackurl}");
1 /** 2 * 获取临时访问口令 3 * @param $pageid 4 * @param $access_token 5 * @return mixed 6 */ 7 public function getpageaccesstoken($pageid, $access_token) 8 { 9 $accesstokenurl = "https://graph.facebook.com/v4.0/{$pageid}?fields=access_token&access_token={$access_token}";10 $pageaccesstokeninfo = curl($accesstokenurl);11 try{12 $pageaccesstokeninfo = json_decode($pageaccesstokeninfo, true);13 $pageaccesstoken = $pageaccesstokeninfo['access_token'];14 }catch (exception $e) {15 $this->showresults(-1, null, '获取page_access_token失败!');16 }17 18 return $pageaccesstoken;19 }
和,权限不同的自行更改!
/** * 获取facebook授权code */ public function bindaction(){ $redirecturl = facebook redirecturl; $appid = facebook appid; $appcret = facebook appcret; $fb = new facebook\facebook([ 'app_id' => $appid, 'app_cret' => $appcret, 'default_graph_version' => 'v2.10', ]); $helper = $fb->getredirectloginhelper(); $permissions = ['manage_pages','publish_pages']; $codeurl = $helper->getloginurl($redirecturl, $permissions); $this->showresults(1, ['url' => $codeurl]); }
/** * facebook分享发布 */ public function facebookshareaction() { $data = getpost('data');//一次请求中只能有一个选项 1,分享链接, 2单张图片链接, 3,多张图片id $content['message'] = $data['message']; if ($data['link']) { $content['link'] = $data['link']; } el if ($data['url']) { $content['link'] = ''; $content['url'] = $data['url']; } el if ($data['attached_media']) { $content['link'] = ''; $content['url'] = ''; $media = explode(',', $data['attached_media']); if (count($media) >= 10) { $this->showresults(-2, null, '一次上传最多不能超过9张图片!'); } foreach ($media as $mediaid) { $temp[] = ['media_fbid' => $mediaid]; } $content['attached_media'] = json_encode($temp); } $appid = facebook appid; $appcrelol卡萨丁t = facebook appcret; $fbaccesstoken = $this->cache->get($this->facebookaccesstokenkey); if (!$fbaccesstoken) { $this->showresults(-1, null, '请先绑定facebook账号!'); } $fb = new facebook\facebook([ 'app_id' => $appid, 'app_cret' => $appcret, 'default_graph_version' => 'v2.3', ]); try { $respon = $fb->get( '/me/accounts', $fbaccesstoken ); } catch(facebookexceptionsfacebookresponexception $e) { echo 'graph returned an error: ' . $e->getmessage(); 考试激励语 exit; } catch(facebookexceptionsfacebooksdkexception $e) { echo 'facebook sdk returned an error: ' . $e->getmessage(); exit; } $accountinfo = $respon->getbody(); $accountinfo = json_decode($accountinfo, true); $pageid = $accountinfo['data'][0]['id']; $accesstoken = $accountinfo['data'][0]['access_token']; $pageaccesstoken = $this->getpageaccesstoken($pageid, $accesstoken); try { $respon = $fb->post( '/' . $pageid . '/feed', $content, $pageaccesstoken ); } catch(facebookexceptionsfacebookresponexception $e) { echo 'graph returned an error: ' . $e->getmessage(); exit; } catch(facebookexceptionsfacebooksdkexception $e) { echo 'facebook sdk returned an error: ' . $e->getmessage(); exit; } $info = $respon->getbody(); $info = json_decode($info, true); $this->showresults(1, ['data' => ['id' => $info['id']]]); }
本文发布于:2023-04-07 13:30:19,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/475514d849ad91b95886b79a02a58967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:系统集成Facebook授权发布帖子以及获取帖子评论等功能.doc
本文 PDF 下载地址:系统集成Facebook授权发布帖子以及获取帖子评论等功能.pdf
留言与评论(共有 0 条评论) |