对于想了解facebooksdkphp示例无效的读者,本文将是一篇不可错过的文章,我们将详细介绍facebook.sdk,并且为您提供关于#import"facebookSDK/FacebookSDK
对于想了解facebook sdk php示例无效的读者,本文将是一篇不可错过的文章,我们将详细介绍facebook.sdk,并且为您提供关于#import
- facebook sdk php示例无效(facebook.sdk)
- #import
"facebookSDK/FacebookSDK.h file not found" - <轉>PHP Facebook SDK 4.0.0案例
- Android Facebook SDK 4.X,如何获取电子邮件地址和Facebook访问令牌以将其传递到Web服务
- android – Facebook SDK 3.0 – 获取Facebook用户名和访问令牌
facebook sdk php示例无效(facebook.sdk)
我正在尝试为一个网站开发facebook登录,我尝试了用PHP-facebook-sdk给出的例子,即使在登录到facebook之后,即使在Facebook登录后它还没有显示注销网址,$user变量仍为0.
拨打$facebook-> getUser();功能总是返回0.
require '../src/facebook.PHP';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we kNow the user is logged into
// Facebook, but we don't kNow if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed kNowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
print_r($user);
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getlogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/verity.vis');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>PHP-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>PHP-sdk</h1>
<?PHP if ($user): ?>
<a href="<?PHP echo $logoutUrl; ?>">logout</a>
<?PHP else: ?>
<div>
Check the login status using OAuth 2.0 handled by the PHP SDK:
<a href="<?PHP echo $statusUrl; ?>">Check the login status</a>
</div>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?PHP echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?PHP endif ?>
<h3>PHP Session</h3>
<pre><?PHP print_r($_SESSION); ?></pre>
<?PHP if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?PHP echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?PHP print_r($user_profile); ?></pre>
<?PHP else: ?>
<strong><em>You are not Connected.</em></strong>
<?PHP endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/verity.vis/picture">
<?PHP echo $naitik['name']; ?>
</body>
</html>
解决方法:
如果没有用户登录,Facebook用户可以返回0.
您拥有用户的情况意味着您有一个用户通过您的应用程序登录到Facebook.
这也并不意味着您有权查看用户的详细信息.试试我前段时间写的这个简单例子,但仍然有用.
现在注意这是一个重定向控制器.如您所见,它只检查并询问用户.将它用作“中间”控制器,并使用JSFBSDK创建模板化HTML逻辑,或者每当您需要用户将操作重定向到此处时(例如登录等)
// 0. We don't need to clear session
require_once realpath(dirname(__FILE__)) . 'facebook.PHP';
$current_url = ''; // replace
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
$fbCurrentUserID = $facebook->getUser();
// 1. If we cant get the user log him in and request permissions This requests combined permissions (basic + post)
if (!$fbCurrentUserID){
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email, publish_actions', // Put the permissions you like
'redirect_uri' => $current_url, // Replace here
));
die(header('Location: ' . $loginUrl));
}
// 2. So we do have a current FB user. Lets try to read data (Maybe he declined perms)
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// 2.b Log any error and retry please
die(header('Location: /'));
}
//Continue your code execution here after all up above went well
#import "facebookSDK/FacebookSDK.h file not found"
第一种可能: 从window下上传到mac 下 facebook sdk出错!【这个是系统的错误,只能从其他地方拷贝 FacebookSDK.framework 文件夹 就ok】,下图是正常状态,【不正常的状态下,这几个文件直接是物理文件了】
第二种可能 ,设置读取路径错误
在mac下 project下找到build setting ,再找 searchPath,点击Framework search paths 设置路径即可
"$(SRCROOT)/xxx/xxx/FacebookSDK.framework"
<轉>PHP Facebook SDK 4.0.0案例
先在根目錄建立 composer.json
內容填入
1
2
3
4
5
|
{
"require"
: {
"facebook/php-sdk-v4"
:
"4.0.*"
}
}
|
接著執行 composer install
,系統會自動建立 vendor 目錄。在 application/libraries
建立 lib_login.php
檔案,並且寫入底下程式碼
<?php
if
(! defined(
''BASEPATH''
))
exit
(
''No direct script access allowed''
);
/**
* Name: Facebook Login Library
*
* Author: appleboy
*
*/
require
''vendor/autoload.php''
;
use
Facebook\FacebookSession;
use
Facebook\FacebookRequest;
use
Facebook\GraphUser;
use
Facebook\FacebookRequestException;
use
Facebook\FacebookRedirectLoginHelper;
class
Lib_login
{
/**
* CodeIgniter global
*
* @var string
**/
protected
$ci
;
/**
* __construct
*
* @return void
* @author Ben
**/
public
function
__construct()
{
$this
->ci =& get_instance();
$this
->ci->load->library(
''session''
);
$this
->ci->config->load(
''facebook''
);
if
(! isset(
$_SESSION
)) {
session_start();
}
}
public
function
facebook()
{
$facebook_default_scope
=
explode
(
'',''
,
$this
->ci->config->item(
"facebook_default_scope"
));
$facebook_app_id
=
$this
->ci->config->item(
"facebook_app_id"
);
$facebook_api_secret
=
$this
->ci->config->item(
"facebook_api_secret"
);
// init app with app id and secret
FacebookSession::setDefaultApplication(
$facebook_app_id
,
$facebook_api_secret
);
// login helper with redirect_uri
$helper
=
new
FacebookRedirectLoginHelper(site_url(
''login/facebook''
));
// see if a existing session exists
if
(isset(
$_SESSION
) && isset(
$_SESSION
[
''fb_token''
])) {
// create new session from saved access_token
$session
=
new
FacebookSession(
$_SESSION
[
''fb_token''
]);
// validate the access_token to make sure it''s still valid
try
{
if
(!
$session
->validate()) {
$session
= null;
}
}
catch
(Exception
$e
) {
// catch any exceptions
$session
= null;
}
}
if
(!isset(
$session
) ||
$session
=== null) {
// no session exists
try
{
$session
=
$helper
->getSessionFromRedirect();
}
catch
(FacebookRequestException
$ex
) {
// When Facebook returns an error
// handle this better in production code
print_r(
$ex
);
}
catch
(Exception
$ex
) {
// When validation fails or other local issues
// handle this better in production code
print_r(
$ex
);
}
}
// see if we have a session
if
(isset(
$session
)) {
// save the session
$_SESSION
[
''fb_token''
] =
$session
->getToken();
// create a session using saved token or the new one we generated at login
$session
=
new
FacebookSession(
$session
->getToken());
// graph api request for user data
$request
=
new
FacebookRequest(
$session
,
''GET''
,
''/me''
);
$response
=
$request
->execute();
// get response
$graphObject
=
$response
->getGraphObject()->asArray();
$fb_data
=
array
(
''me''
=>
$graphObject
,
''loginUrl''
=>
$helper
->getLoginUrl(
$facebook_default_scope
)
);
$this
->ci->session->set_userdata(
''fb_data''
,
$fb_data
);
}
else
{
$fb_data
=
array
(
''me''
=> null,
''loginUrl''
=>
$helper
->getLoginUrl(
$facebook_default_scope
)
);
$this
->ci->session->set_userdata(
''fb_data''
,
$fb_data
);
}
return
$fb_data
;
}
}
最後寫簡單 controller
<?php
if
( ! defined(
''BASEPATH''
))
exit
(
''No direct script access allowed''
);
class
Login
extends
CI_Controller
{
public
function
__construct()
{
parent::__construct();
$this
->load->library(
array
(
''session''
,
''lib_login''
));
}
/**
* facebook login
*
* @return void
* @author appleboy
**/
public
function
facebook()
{
$fb_data
=
$this
->lib_login->facebook();
// check login data
if
(isset(
$fb_data
[
''me''
])) {
var_dump(
$fb_data
);
}
else
{
echo
''<a href="''
.
$fb_data
[
''loginUrl''
] .
''">Login</a>''
;
}
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
打開瀏覽器,直接執行 http://xxxx/login/facebook
就可以看到 Facebook 登入連結。所以程式碼都放在 Github 上 codeigniter-facebook-php-sdk-v4,歡迎取用。
Android Facebook SDK 4.X,如何获取电子邮件地址和Facebook访问令牌以将其传递到Web服务
编辑:我的问题是如何使用Facebook SDK 4.X获取电子邮件,UserId,Facebook身份验证,此刻,借助Ming
Respond,我知道如何获取电子邮件,用户ID,所以我的问题是如何获取Facebook身份验证,因为Session和GraphUser刚被LoginManager和AccessToken取代,没有关于它的信息吗?
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import com.facebook.AccessToken;import com.facebook.AccessTokenTracker;import com.facebook.CallbackManager;import com.facebook.FacebookCallback;import com.facebook.FacebookException;import com.facebook.Profile;import com.facebook.ProfileTracker;import com.facebook.login.LoginResult;import com.facebook.login.widget.LoginButton;import java.util.Arrays;public class RegisterActivity extends Activity { private String fbUserID; private String fbProfileName; private String fbAuthToken; private LoginButton fbLoginBtn; private static final String TAG = "FacebookLogin"; CallbackManager callbackManager; private AccessTokenTracker accessTokenTracker; private ProfileTracker profileTracker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register_activity); fbLoginBtn = (LoginButton) findViewById(R.id.connect_with_facebook_button); fbLoginBtn.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile")); fbLoginBtn.setBackgroundResource(R.drawable.connect_facebook_button); accessTokenTracker = new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged( AccessToken oldAccessToken, AccessToken currentAccessToken) { fbAuthToken = currentAccessToken.getToken(); fbUserID = currentAccessToken.getUserId(); Log.d(TAG, "User id: " + fbUserID); Log.d(TAG, "Access token is: " + fbAuthToken); } }; profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged( Profile oldProfile, Profile currentProfile) { fbProfileName = currentProfile.getName(); Log.d(TAG, "User name: " + fbProfileName ); } }; fbLoginBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);callbackManager.onActivityResult(requestCode, resultCode, data); }GraphRequest request = GraphRequest.newMeRequest( accessToken, new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject user, GraphResponse response) { String id = user.optString("id"); String firstName = user.optString("first_name"); String lastName = user.optString("last_name"); String email = user.optString("email"); } @Override public void onSaveInstanceState(Bundle savedState) { super.onSaveInstanceState(savedState); }
答案1
小编典典fbLoginBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject me, GraphResponse response) { if (response.getError() != null) { // handle error } else { String email = me.optString("email"); String id = me.optString("id"); // send email and id to your web server } } }).executeAsync(); } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code }});
android – Facebook SDK 3.0 – 获取Facebook用户名和访问令牌
我正在跟随facebook本地登录 – http://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/
我使用Session.getAccesstoken获取访问令牌,我得到一些访问令牌,但是这是无效的.实际程序是什么?我做错了吗
如何使用Facebook SDK 3.0在Native登录中获取UserId
解决方法
final Session session = Session.getActiveSession(); if (session != null && session.isOpened()) { // If the session is open,make an API call to get user data // and define a new callback to handle the response Request request = Request.newMeRequest(session,new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user,Response response) { // If the response is successful if (session == Session.getActiveSession()) { if (user != null) { user_ID = user.getId();//user id profileName = user.getName();//user's profile name userNameView.setText(user.getName()); } } } }); Request.executeBatchAsync(request); }
user_ID& profileName是字符串.
对于accesstoken:
String token = session.getAccesstoken();
EDITED:(13/1/2014)
对于用户电子邮件(我没有通过在设备或模拟器上运行来检查此代码):
这些只是我的意见,或者你可以称之为建议
setReadPermissions(Arrays.asList("email",...other permission...)); //by analyzing the links bellow,i think you can set the permission in loginbutton as: loginButton.setReadPermissions(Arrays.asList("email",...other permission...)); user.asMap().get("email");
更多信息请看:
link1,link2,link3,link4,
今天关于facebook sdk php示例无效和facebook.sdk的讲解已经结束,谢谢您的阅读,如果想了解更多关于#import
本文标签: