Search Unity

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

    Wuhoo404

    Joined:
    Feb 24, 2023
    Posts:
    2
    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;
    9.  
    10. private static DriveService GetService()
    11. {
    12.     var tokenResponse = new TokenResponse
    13.     {
    14.         AccessToken = "...",
    15.         RefreshToken = "..."
    16.     };
    17.  
    18.     var username = "google account email";
    19.  
    20.     var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    21.     {
    22.         ClientSecrets = new ClientSecrets
    23.         {
    24.             ClientId = "...",
    25.             ClientSecret = "..."
    26.         },
    27.     });
    28.  
    29.     var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
    30.  
    31.     var service = new DriveService(new BaseClientService.Initializer
    32.     {
    33.         HttpClientInitializer = credential,
    34.         ApplicationName = applicationName
    35.     });
    36.  
    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 :)