如何在 C# 中为 android 构建获取 OAuth 2.0 访问令牌

问题描述 投票:0回答:0

我正在关注 Google 授权 OAuth2 以获取我的 Android 客户端 ID 的访问令牌。但我不打算将我的游戏上传到 Google Play 平台。我只想将应用程序生成的文件上传到我的驱动器帐户。

是否可以在不征得用户同意并输入登录信息的情况下这样做? 我在网上收集的大量信息正在与 Google Play 控制台通信,我不需要登录/安全信息,因为我将是唯一的用户。 ..

我用 Web 应用程序客户端 ID 做了一些实验,它以我想要的方式工作。这是代码的一部分:

using System.IO;
using UnityEngine;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;

private static DriveService GetService()
{
    var tokenResponse = new TokenResponse
    {
        AccessToken = "...",
        RefreshToken = "..."
    };

    var username = "google account email";

    var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecrets = new ClientSecrets
        {
            ClientId = "...",
            ClientSecret = "..."
        },
    });

    var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
    
    var service = new DriveService(new BaseClientService.Initializer
    {
        HttpClientInitializer = credential,
        ApplicationName = applicationName
    });

    return service;
}

让我感到困惑的另一件事是,在创建 Web 客户端 ID 时,我可以同时获得 clientId 和 clientSecret。通过这些,我可以通过 OAuth 2.0 Playground 轻松获得令牌。但是当我为安卓创建client id时,我只能得到client id。我不确定我应该做什么才能更进一步......

我是 google api 的初学者,这也是我的第一个 Android 游戏!请(请)帮忙,如果有任何见解,我将不胜感激!

谢谢:-)

c# android unity3d google-drive-api google-oauth
© www.soinside.com 2019 - 2024. All rights reserved.