Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, could not create txt file on google drive. code throwing exception. Help will be deeply appreciated.

What I have tried:

using System;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
    internal class Program
        static async Task Main(string[] args)
            var credentials = await GoogleCredential.FromFileAsync("key/mobileprice-389203-a6dc6ada2693.json", CancellationToken.None);
            var service = new DriveService(new BaseClientService.Initializer());
            var metadata = new Google.Apis.Drive.v3.Data.File() { Name = "myfile.txt" };
            //var mediacontent = new MemoryStream(System.IO.TextWriter("hello world"));
            //var uploadRequest = service.Files.Create(metadata, mediacontent, "text/plain");
            var uploadRequest = service.Files.Create(metadata);
            uploadRequest.Fields = "id";
            var file = uploadRequest.Execute();
            Console.WriteLine($"File ID: {file.Id}");

service drive exception http status code is un authorise request is missing required authorisation credential expected auth2 access token
Updated 8-Jun-23 8:34am
PIEBALDconsult 8-Jun-23 10:07am    
Create it locally and then move it.
Richard MacCutchan 8-Jun-23 10:14am    
Another clear error message: you need to be authorised.
suhail malik 2023 8-Jun-23 11:04am    
. am i missing some thing in credential filebut service runs successfully
Richard MacCutchan 8-Jun-23 11:07am    
Check the documentation for the Google service you are using.

1 solution

I don't know anything about Google service API's since I have no need to use them, just looking at your code, it would appear that you're creating a credential object, but you never use it or pass it to any service function. That would probably be why you're getting the error message.

You're going to have to read the documentation on the service you're trying to use so you can understand how to pass credentials or your credential object to it, thereby authorizing the call.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900