1
  my $fb = Net::Facebook::Oauth2->new(
        application_id => 'your_application_id',
        application_secret => 'your_application_secret',
        callback => 'http://yourdomain.com/facebook/callback'
    );

    my $access_token = $fb->get_access_token(code => $cgi->param('code'));
    ###save this token in database or session

    ##later on your application you can use this verifier code to comunicate
    ##with facebook on behalf of this user

    my $fb = Net::Facebook::Oauth2->new(
        access_token => $access_token
    );

    my $info = $fb->get(
        'https://graph.facebook.com/me' ##Facebook API URL
    );

    print $info->as_json;

当我尝试打印响应的 json 格式时,我错过了电子邮件,以下是我得到的输出

{"id":"100001199655561","name":"Pavan Kumar Tummalapalli","first_name":"Pavan","middle_name":"Kumar","last_name":"Tummalapalli","link":"http:\/\/www.facebook.com\/pavan.tummalapalli","username":"pavan.tummalapalli","hometown":{"id":"125303864178019","name":"Kodada, India"},"location":{"id":"115200305158163","name":"Hyderabad, Andhra Pradesh"},"favorite_athletes":[{"id":"108661895824433","name":"AB de Villiers"}],"education":[{"school":{"id":"129163957118653","name":"City Central School"},"type":"High School"},{"school":{"id":"124833707555779","name":"Anurag Engineering College"},"year":{"id":"136328419721520","name":"2009"},"type":"College"}],"gender":"male","timezone":5.5,"locale":"en_US","verified":true,"updated_time":"2012-12-30T09:13:54+0000"}

' https://graph.facebook.com/me?fields=email

我得到以下回复

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
4

1 回答 1

1

可能您没有足够的权限来访问该数据。您提供的代码并未表明您是否在授权过程中请求了电子邮件权限,所以我猜您没有请求它。

当您将用户重定向到Auth Dialog时,例如https://www.facebook.com/dialog/oauth/?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URL&state=YOUR_STATE_VALUE&scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES,您必须指定 scope=email 以获得访问电子邮件的权限场地。

要检查您是否拥有权限,您可以访问下面的 URL 并查看您是否拥有。

my $permission_ref = $fb->get(
    'https://graph.facebook.com/me/permissions'
);

返回值应如下所示。

{
  "data": [
    {
      "installed": 1, 
      "email": 1
    }
  ]
}

如果包含电子邮件,则您有权访问用户电子邮件。

如果不是,您必须请求许可才能获得它。使用 Net::Facebook::OAuth2,您可以生成此对话框 URL,如下所示。

  my $fb = Net::Facebook::Oauth2->new(
      application_id     => 'your_application_id', 
      application_secret => 'your_application_secret',
      callback           => 'http://yourdomain.com/facebook/callback'
  );

  my $url = $fb->get_authorization_url(
      scope => ['email'],
  );

将您的用户重定向到此网址,您将获得许可。

即使您有电子邮件权限,有时电子邮件也不会因为错误而返回。您可能想查看这个错误报告,“ API call to /me is missing user's email even with email permission ”。

于 2013-06-07T12:50:43.970 回答