一旦我已经拥有访问令牌,如何使用 php 在服务器上获取 Facebook 用户电子邮件
How to get Facebook user email on server with php once I already have the access token
我成功地从我的 Android 应用程序中获得了访问令牌...
fbbutton = (com.facebook.login.widget.LoginButton) findViewById(R.id.fbbutton);
fbbutton.setOnClickListener(this);
fbbutton.setReadPermissions(Arrays.asList("public_profile", "email"));
callbackManager = CallbackManager.Factory.create();
fbbutton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
if (response.getError() != null) {
} else {
if ((email = object.optString("email")) != null) {
} else {
email = object.optString("id") + "@facebook.com";
}
editor.putString("ftoken", AccessToken.getCurrentAccessToken().toString());
editor.apply();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email"); // facebook parameters
request.setParameters(parameters);
request.executeAsync();
// signed in UI
}
然后我成功地将访问令牌发送到我服务器的 php 脚本。
我正在尝试让 facebook 使用访问令牌在服务器端给我用户的电子邮件。
我正在努力...
require_once __DIR__ . '/facebook-php-sdk-v4/src/Facebook/autoload.php';
(...)
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
fwrite($Handle, "point 1");
$response = $fb->get('/me', $ftoken);
//print_r($response);
fwrite($Handle, "response:$response\n\n");
$email = $response["email"];
$userid = $email;
fwrite($Handle, "email confirmed by facebook:[$email]\n\n");
但是我的 php 代码只能到达 "point 1"。所以它在 $fb->get 阶段失败了。我究竟做错了什么?感谢您的帮助。
编辑:我忘了在最后一个数组项后面加逗号
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4',
]);
现在我的代码甚至没有到达 点 1。
编辑:逗号显然无关紧要,但我把它去掉了,我又回到了 点 1 [我错了,逗号真的没有什么区别] .但是 var_dumps 出现空的,因为我在下面编程..
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$vardump = var_dump($fb);
fwrite($Handle, "first vardump:$vardump\n\n");
$response = $fb->get('/me?fields=id,name', $ftoken);
$vardump = var_dump($response);
fwrite($Handle, "vardump:$vardump\n\n");
编辑:#3:我实现了以下建议的代码...
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$fb->setDefaultAccessToken($ftoken);
try {
$response = $fb->get('/me');
$userResponse = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$userResponse = 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$userResponse = 'Facebook SDK returned an error: ' . $e->getMessage();
}finally{
fwrite($Handle, "response:$userResponse\n\n");
}
然后我得到...
response:Graph returned an error: Invalid OAuth access token.
所以我的令牌类型似乎不正确?
编辑:#4:我进入了 facebook 对象调试页面并输入了令牌...
Access Token Info
App ID xxxxxxxxxxxxxxxxx : recipes
User ID
xxxxxxxxxxxxxxxxxxxxxxxxx : Rxxn Kxxm
User last installed this app via API v2.x
Issued 1451792399 (20 hours ago)
Expires 1456976399 (in about 2 months)
Valid True
Origin Mobile Web Faceweb
Scopes email, public_profile
所以令牌应该没有任何实际问题。我被难住了。
您可以尝试以下方法
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$fb->setDefaultAccessToken($ftoken);
try {
$response = $fb->get('/me');
$userResponse = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$userResponse = 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$userResponse = 'Facebook SDK returned an error: ' . $e->getMessage();
}finally{
fwrite($Handle, "response:$userResponse\n\n");
}
看看我们得到了什么...
确保您的域在您的应用设置中!
我成功地从我的 Android 应用程序中获得了访问令牌...
fbbutton = (com.facebook.login.widget.LoginButton) findViewById(R.id.fbbutton);
fbbutton.setOnClickListener(this);
fbbutton.setReadPermissions(Arrays.asList("public_profile", "email"));
callbackManager = CallbackManager.Factory.create();
fbbutton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
if (response.getError() != null) {
} else {
if ((email = object.optString("email")) != null) {
} else {
email = object.optString("id") + "@facebook.com";
}
editor.putString("ftoken", AccessToken.getCurrentAccessToken().toString());
editor.apply();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email"); // facebook parameters
request.setParameters(parameters);
request.executeAsync();
// signed in UI
}
然后我成功地将访问令牌发送到我服务器的 php 脚本。
我正在尝试让 facebook 使用访问令牌在服务器端给我用户的电子邮件。
我正在努力...
require_once __DIR__ . '/facebook-php-sdk-v4/src/Facebook/autoload.php';
(...)
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
fwrite($Handle, "point 1");
$response = $fb->get('/me', $ftoken);
//print_r($response);
fwrite($Handle, "response:$response\n\n");
$email = $response["email"];
$userid = $email;
fwrite($Handle, "email confirmed by facebook:[$email]\n\n");
但是我的 php 代码只能到达 "point 1"。所以它在 $fb->get 阶段失败了。我究竟做错了什么?感谢您的帮助。
编辑:我忘了在最后一个数组项后面加逗号
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4',
]);
现在我的代码甚至没有到达 点 1。
编辑:逗号显然无关紧要,但我把它去掉了,我又回到了 点 1 [我错了,逗号真的没有什么区别] .但是 var_dumps 出现空的,因为我在下面编程..
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$vardump = var_dump($fb);
fwrite($Handle, "first vardump:$vardump\n\n");
$response = $fb->get('/me?fields=id,name', $ftoken);
$vardump = var_dump($response);
fwrite($Handle, "vardump:$vardump\n\n");
编辑:#3:我实现了以下建议的代码...
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$fb->setDefaultAccessToken($ftoken);
try {
$response = $fb->get('/me');
$userResponse = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$userResponse = 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$userResponse = 'Facebook SDK returned an error: ' . $e->getMessage();
}finally{
fwrite($Handle, "response:$userResponse\n\n");
}
然后我得到...
response:Graph returned an error: Invalid OAuth access token.
所以我的令牌类型似乎不正确?
编辑:#4:我进入了 facebook 对象调试页面并输入了令牌...
Access Token Info
App ID xxxxxxxxxxxxxxxxx : recipes
User ID
xxxxxxxxxxxxxxxxxxxxxxxxx : Rxxn Kxxm
User last installed this app via API v2.x
Issued 1451792399 (20 hours ago)
Expires 1456976399 (in about 2 months)
Valid True
Origin Mobile Web Faceweb
Scopes email, public_profile
所以令牌应该没有任何实际问题。我被难住了。
您可以尝试以下方法
$ftoken = $json->ftoken;
fwrite($Handle, "ftoken:$ftoken\r\n");
$fb = new Facebook\Facebook([
'app_id' => '491680537671890',
'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
'default_graph_version' => 'v2.4'
]);
$fb->setDefaultAccessToken($ftoken);
try {
$response = $fb->get('/me');
$userResponse = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$userResponse = 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$userResponse = 'Facebook SDK returned an error: ' . $e->getMessage();
}finally{
fwrite($Handle, "response:$userResponse\n\n");
}
看看我们得到了什么...
确保您的域在您的应用设置中!