Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase AuthenticationのTwitter認証Sample.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using TwitterKit.Unity;
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
public class PUFirebaseTwitterLogin : MonoBehaviour {
private string _AccessToken;
private string _Secret;
private string _UserName;
void Start () {
Twitter.Init();
this.TwitterAuth();
}
public void TwitterAuth() {
TwitterSession session = Twitter.Session;
if (session == null) {
Twitter.LogIn(TwitterLoginComplete, TwitterLoginFailure);
} else {
TwitterLoginComplete(session);
}
}
public void TwitterLoginComplete(TwitterSession session) {
Debug.Log("[Info] : Login success. " + session.authToken);
_AccessToken = session.authToken.token;
_Secret = session.authToken.secret;
_UserName = session.userName;
this.FirebaseLogin();
}
public void TwitterLoginFailure(ApiError error) {
Debug.Log("[Error ] : Login faild code =" + error.code + " msg =" + error.message);
}
public void FirebaseLogin() {
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential = Firebase.Auth.TwitterAuthProvider.GetCredential(_AccessToken, _Secret);
auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("[Info ] : SignInWithCredentialAsync canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("[Error ] : SignInWithCredentialAsync fatal. an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.