3

実際にログインせずに User および UserSocialAuth オブジェクトを作成する方法はありますが、アカウントを作成したいユーザーの FACEBOOK_ID しか持っていませんか?

ユーザーが FB から友人を選択し、彼のためにモデル オブジェクトを作成できるシンプルなアプリを作成しています。私は User エンティティへの参照を持っているので、その友人が登録されているかどうかにかかわらず、それを持っている必要があります。そうでない場合は、オブジェクト グラフ全体をプログラムで作成する必要があります。django-social-auth の標準機能を使用して行うことは可能ですか、それとも「auth_user」と「social_auth_usersocialauth」に手動でレコードを作成する必要がありますか?

4

2 に答える 2

3

ユーザー名をその人の FACEBOOK_ID に設定するこのカスタム バックエンドを使用します。

from social_auth.backends.facebook import FacebookBackend
class IDFacebookBackend(FacebookBackend):
    """Facebook OAuth2 authentication backend"""
    def get_user_details(self, response):
        """Return user details from Facebook account"""
        return {'username': response.get('id'),
                'email': response.get('email', ''),
                'fullname': response.get('name', ''),
                'first_name': response.get('first_name', ''),
                'last_name': response.get('last_name', '')}

get_usernameの代わりに、認証パイプラインでこのバージョンの を使用しますsocial_auth.backends.pipeline.user.get_username

def get_username(details, user=None, *args, **kwargs):
    " Make Username from User Id "
    if user:
        return {'username': UserSocialAuth.user_username(user)}
    else:
        return details['username']

パイプラインは次のようになります。

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'our_custom_auth.get_username', # <= This should be the previous function
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details'
)

次に、呼び出すUser.objects.create(username=friends_facebook_id)だけで、ユーザー名/パスでログインできないユーザーがいますが、ForeignKey フィールドから簡単に参照できます。

また、後日その「友達」があなたのサイトに来て (SocialAuth を使用して) 参加すると、自動的にこの User オブジェクトが与えられ、内部グラフは正確に保たれます。

于 2013-04-08T06:27:16.237 に答える
0

したがって、上記のアイデアを使用しましたが、労力は少なく、最終的には次のようになりました。

associate_by_emailステージを からSOCIAL_AUTH_PIPELINEの独自の実装 にオーバーライドしましたassociate_by_username。ユーザー名は Django auth で一意であるため、それを使用できます。したがって、完全な変更は

  1. 埋め込むassociate_by_username

    django.core.exceptions からインポート MultipleObjectsReturned、ObjectDoesNotExist social_auth.exceptions からインポート AuthException から django.contrib.auth.models インポート ユーザー

    def Associate_by_username(details, user=None, *args, **kwargs): """詳細で返されたものと同じ電子メール アドレスを持つユーザー エントリを返します。""" if user: return None

    username = details.get('username')
    
    if username:
        try:
            return {'user': User.objects.get(username=username)}
        except MultipleObjectsReturned:
            raise AuthException(kwargs['backend'], 'Not unique email address.')
        except ObjectDoesNotExist:
            pass
    
  2. 次に、このメソッドをパイプラインに追加します

    SOCIAL_AUTH_PIPELINE = ( 'social_auth.backends.pipeline.social.social_auth_user', 'core.auth.associate_by_username', 'social_auth.backends.pipeline.user.get_username', 'social_auth.backends.pipeline.user.create_user', 'social_auth. backends.pipeline.social.associate_user', 'social_auth.backends.pipeline.social.load_extra_data', 'social_auth.backends.pipeline.user.update_user_details', )

  3. ユーザーを取得する必要がある場合は、facebookId() で検索しますが、facebookUsername( )recipient = UserSocialAuth.objects.filter(uid=target_facebook_id) で作成します。User(username=target_username)

于 2013-06-08T12:37:57.117 に答える