2013-05-21 49 views
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格式輸出什麼,我得到不能得到電子郵件的Net :: Facebook的::的oauth2

{"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

也許你沒有足夠的權限交流放棄這些數據。您提供的代碼並不表明您是否在授權過程中要求email permission,所以我猜你沒有要求它。

當您將用戶重定向到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'], 
); 

將您的用戶重定向到此URL,您將獲得權限。

即使您有電子郵件許可,有時電子郵件也不會因爲錯誤而返回。你可能想檢查這個錯誤報告,「API call to /me is missing user's email even with email permission」。

+0

真的感謝您的回覆..實際上,我發佈了1小時後,我添加範圍'電子郵件'的解決方案,因爲你在你的解決方案中提到.. – pavan