2013-02-22 60 views
12

我正在嘗試獲取經過身份驗證的Facebook用戶的個人資料圖片,以便在Meteor應用程序中使用。我試過以下如何通過Meteor的accounts-facebook查詢Facebook用戶圖片?

Meteor.publish("facebook_avatar_url", function() { 
    return Meteor.users.find({_id: this.userId}, {fields: { 
     'services.facebook.id': 1, 
     'services.facebook.name': 1, 
     'services.facebook.gender': 1, 
     'services.facebook.picture': 1, 
     'services.facebook.picture.data': 1, 
     'services.facebook.picture.data.url': 1 
    }}); 
}); 

它只返回id,名稱和性別。這似乎是我想要的,以及推薦的解決方案。唯一的問題是沒有關於用戶圖片的數據被返回。

我嘗試在server/server.js中添加以下內容,但是它a)似乎不是推薦的方法,並且b)似乎沒有做任何事情。所以,這似乎是一個死路一條,但有人似乎認爲它可以用來加載配置文件圖片。

var getFbPicture; 

Accounts.loginServiceConfiguration.remove({ 
    service: "facebook" 
}); 


Accounts.onCreateUser(function(options, user) { 
    if (options.profile) { 
     options.profile.picture = getFbPicture(user.services.facebook.accessToken); 
     user.profile = options.profile; 
    } 
    return user; 
}); 

getFbPicture = function(accessToken) { 
    var result; 
    result = Meteor.http.get("https://graph.facebook.com/me", { 
     params: { 
      access_token: accessToken, 
      fields: 'picture' 
     } 
    }); 
    if (result.error) { 
     throw result.error; 
    } 
    return result.data.picture.data.url; 
}; 

所以,我有點不確定這個時候走哪個方向。這是否需要在Facebook Graph API上設置權限?或在Facebook應用程序?我在發佈函數中有錯誤的語法嗎?我需要重新訪問onCreateUser函數嗎?

回答

36

取而代之,你不需要訪問令牌或任何特殊的東西來獲得他們的profile picture,只是他們的facebook用戶ID。

Accounts.onCreateUser(function(options, user) { 
    if (options.profile) { 
     options.profile.picture = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large"; 
     user.profile = options.profile; 
    } 
    return user; 
}); 
+3

感謝您的反饋Akshat 。 Accounts.onCreateUser方法實際上破壞了我的應用程序的用戶引導,但總體思路奏效,因此接受了答案。我結束使用以下內容: 'Template.userCardTemplate.user_image = function(){ if(Meteor.user()。services.facebook){ return「http://graph.facebook.com/」+流星.user()。services.facebook.id +「/ picture /?type = large」; } };' – AbigailW 2013-02-22 17:53:56

+0

請我希望你能夠像你想要的那樣使用它!它不應該打破你的應用程序,你是否在服務器端代碼中使用它?你的控制檯是否出現錯誤?註冊時是否傳遞選項? – Akshat 2013-02-22 18:00:47

+0

你的建議是好的代碼,並正確地使用框架。在引導用戶時,我碰巧繞過了帳戶模塊,並將它們直接插入到用戶集合中。錯誤在我身邊,因爲我使用輔助的非標準機制來引導用戶。我必須重新考慮那個引導文件,並找出如何使用Accounts模塊重構它。 (甚至可以提交'todos'示例的錯誤修復程序,以使其與帳戶模塊的最新開發速度保持一致。) – AbigailW 2013-02-22 18:16:56

3

,如果你想獲得的圖片爲Facebook

Accounts.onCreateUser(function(options, user) { 
    if (typeof(user.services.facebook) != "undefined") { 
     user.services.facebook.picture = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large"; 
    } 
    return user; 
}); 

你可以在你的HTML添加此輔助函數

UI.registerHelper("getImageUser", function (userId) { 
    var user= Meteor.users.findOne(userId); 
    if (user.services) 
    { 
     if (user.services.facebook) 
      return user.services.facebook.picture; 
     if (user.services.twitter) 
      return user.services.twitter.profile_image_url; 
     if (user.services.google) 
      return user.services.google.picture; 
    } 
    else 
    { 
     return "images/withOutPhoto.png"; 
    } 
}); 

<img src="{{getImageUser this._id}}" alt="..."> 
+1

別忘了: user.profile = options.profile; 這一個將會覆蓋默認的那個,並且你將失去登錄/註冊的名字。那麼它爲我做了^^ – 2014-11-02 21:09:59

相關問題