2013-02-22 9 views
12

私は認証されたFacebookユーザーのプロフィール画像をMeteorアプリケーション内で使用しようとしています。私は以下を試しましたMeteor's 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

感謝をこのヘルパー関数を追加することができます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; }); 

のための画像を取得したい場合。 Accounts.onCreateUserメソッドは、実際に私のアプリケーションのユーザーのブートストラップを壊しましたが、一般的な考え方が働いていたので、答えを受け入れました。 'Template.userCardTemplate.user_image = function(){ if(Meteor.user()。services.facebook){ 返信" http://graph.facebook.com/ "+流星.user()。services.facebook.id + "/ picture /?type = large"; } }; ' – AbigailW

+0

私はあなたが望むようにそれを使用できるようにしてください!それはあなたのアプリを壊すべきではありません、あなたはサーバー側のコードでそれを使用していますか?あなたはあなたのコンソールにエラーを表示しますか?登録時にオプションを渡していますか? – Akshat

+0

提案したものは良いコードであり、フレームワークを正しく使用しています。私はちょうどユーザーをブートストラップするときにAccountsモジュールをバイパスして、Usersコレクションに直接挿入しています。エラーは私の側にあります。なぜなら、ユーザーをブートストラップするための非標準的な第2のメカニズムを使用しているからです。そのブートストラップファイルを再考し、アカウントモジュールを使用してそれをリファクタリングする方法を理解する必要があります。 (「todos」の例のバグフィックスを提出して、Accountsモジュールの最新動向に対応できるようにしてください) – AbigailW

3

あなたの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"; 
    } 
}); 

フィードバックAkshatため

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

忘れないでください: user.profile = options.profile; これはデフォルトのものを上書きし、サインイン/アップの名前が失われます。まあそれは私のためにしました^^ –

関連する問題