Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Feature Request How to obtain OAuth 2.0 access tokens for android build in C#

Discussion in 'Authentication' started by Wuhoo404, May 2, 2023.

  1. Wuhoo404


    Feb 24, 2023
    I am following Google Authorization OAuth2 to get an access token for my Android client ID. But I don't plan to upload my game to the Google Play platform. All I want to do is to upload files generated by the application to my drive account.

    Is it possible to do that without asking user's consent and inputting login information? Lots of information I gathered online is communicating with Google Play Console, and I don't need the login/security stuff since I will be the only user...

    I did some experiments with a web application client ID, and it works in the way that I want. Here is part of the code:
    Code (CSharp):
    1. using System.IO;
    2. using UnityEngine;
    3. using Google.Apis.Auth.OAuth2;
    4. using Google.Apis.Auth.OAuth2.Responses;
    5. using Google.Apis.Auth.OAuth2.Flows;
    6. using Google.Apis.Drive.v3;
    7. using Google.Apis.Services;
    8. using Google.Apis.Util.Store;
    10. private static DriveService GetService()
    11. {
    12.     var tokenResponse = new TokenResponse
    13.     {
    14.         AccessToken = "...",
    15.         RefreshToken = "..."
    16.     };
    18.     var username = "google account email";
    20.     var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    21.     {
    22.         ClientSecrets = new ClientSecrets
    23.         {
    24.             ClientId = "...",
    25.             ClientSecret = "..."
    26.         },
    27.     });
    29.     var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
    31.     var service = new DriveService(new BaseClientService.Initializer
    32.     {
    33.         HttpClientInitializer = credential,
    34.         ApplicationName = applicationName
    35.     });
    37.     return service;
    38. }
    The other thing that confuses me is I can get both clientId and clientSecret when creating the web client id. And through those, I can get tokens easily through OAuth 2.0 Playground. But when I created the client id for Android, I could only get the client id. I am not sure what I should do to go further...

    I am a beginner of google api stuff and this is my first Android game too! Please please (PLEASE) help and I'd appreciate any insights!!

    Thank you :)