【问题标题】:Firebase IDToken to Google OAuth2 access token in UnityFirebase IDToken 到 Unity 中的 Google OAuth2 访问令牌
【发布时间】:2020-09-15 10:01:49
【问题描述】:

我们在 Unity 游戏中使用 Firebase Auth 和 Google SignIn,需要访问 Google 服务,例如 Google Classroom。

Firebase Auth 在使用此代码https://firebase.google.com/docs/auth/admin/verify-id-tokens#unity 登录后为我们提供了一个 JWT IdToken

Firebase.Auth.FirebaseUser newUser = task2.Result;
var token = await newUser.TokenAsync(false); 

我尝试将此令牌传递给 Google 课堂服务,但它需要 OAuth2 AccessToken。

如何将此 JWT Firebase IdToken 转换为可供 GoogleClassroom API 使用的 AccessToken?

我正在寻找要传递给 Google Classroom API 的 AccessToken

var credential = GoogleCredential.FromAccessToken(AccessToken);
var service = new ClassroomService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

我已经尝试过answer 这个问题,但服务返回 404。

【问题讨论】:

    标签: firebase unity3d google-oauth google-authentication


    【解决方案1】:

    您正在尝试获取 google 访问令牌..! 您可以通过简单的 API 调用来获取它。

    IEnumerator GetAccessToken()
            {
                WWWForm form = new WWWForm();
                form.AddField("client_id", "your_webClientId");
                form.AddField("client_secret", "your_clientsecret");
                form.AddField("grant_type", "authorization_code");
                form.AddField("code", "your_authcode);
                UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/oauth2/v4/token", form);
                yield return www.SendWebRequest();
    
                if (www.isNetworkError || www.isHttpError)
                {
                }
                else
                {
    
                    //Access token response
                    GoogleData googleData =JsonUtility.FromJson<GoogleData>(www.downloadHandler.text); 
                    Debug.LogError(googleData.access_token);
                    
                }
            }
    

    在 API 成功后,您可以将响应解析为数据模型类。 注意:不要更改“grant_type”字段值。

    [System.Serializable]
        public class GoogleData
        {
            public string access_token;
            public string token_type;
            public int expires_in;
            public string refresh_token;
        }
    

    【讨论】:

    猜你喜欢
    • 2016-03-05
    • 2015-10-12
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多