无法通过Net :: Facebook :: Oauth2收到电子邮件

时间:2013-05-21 14:27:16

标签: perl facebook-graph-api

  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}}

1 个答案:

答案 0 :(得分:1)

您可能没有足够的权限来访问该数据。您提供的代码并未表明您是否已在授权过程中请求email permission,因此我猜您没有请求它。

当您将用户重定向到Auth Dialog时,您需要指定scope = email以获取访问电子邮件字段的权限。

要检查您是否拥有该权限,可以访问以下网址,看看是否拥有该网址。

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

返回值应如下所示。

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

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

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

  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'],
  );

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

即使您拥有电子邮件权限,有时也会因为错误而退回电子邮件。您可能需要查看此错误报告“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。”