简体   繁体   English

一旦拥有访问令牌,如何使用php在服务器上获取Facebook用户电子邮件

[英]How to get Facebook user email on server with php once I already have the access token

I successfully got the access token from my Android app as such... 我成功地从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
    }

I then successfully sent the access token to my server's php script. 然后,我成功地将访问令牌发送到服务器的php脚本。

I'm trying to get facebook to give me the user's email on the server side using the access token. 我正在尝试让Facebook使用访问令牌在服务器端向我发送用户的电子邮件。

I'm trying as such... 我正在这样尝试...

        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");

But my php code only gets to "point 1". 但是我的php代码只能到达“ point 1”。 So it is failing at the $fb->get stage. 因此它在$ fb-> get阶段失败了。 What am I doing wrong? 我究竟做错了什么? Thanks for any help. 谢谢你的帮助。

Edit: I had forgotten to put a comma after the last array item 编辑:我忘了在最后一个数组项后面加上逗号

$fb = new Facebook\Facebook([
            'app_id' => '491680537671890',
            'app_secret' => 'abe9f51094b3d7c9edb3c28d102170a1',
            'default_graph_version' => 'v2.4',
        ]);

Now my code doesn't even get to point 1 . 现在我的代码甚至没有指向第1点

Edit: Comma apparently doesn't matter but I took it out and I get to point 1 again [I was mistaken and the comma really doesn't make a difference]. 编辑:逗号显然没关系,但是我把它取出来,然后又指向1 [我弄错了,逗号真的没有任何作用]。 But the var_dumps come up empty as I programmed below.. 但是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");

Edit: #3: I implemented the following suggested code... 编辑:#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");
            }

and I get ... 我得到...

response:Graph returned an error: Invalid OAuth access token.

So my token seems to be the incorrect type? 所以我的令牌似乎是错误的类型?

Edit: #4: I went to the facebook object debug page and entered the 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

So there shouldn't be any real problem with the token. 因此令牌应该没有任何实际问题。 I'm stumped. 我很沮丧

You might try the following 您可以尝试以下

$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");
}

and see what we get... 看看我们得到了什么...

Make sure your domain is in your app settings! 确保您的域在您的应用设置中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM