Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cornea Cristian #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tema 3
cristi1990an committed Oct 28, 2021

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 82792501f54adbf7c7b3379d4e19bd12ec4f23a7
12 changes: 12 additions & 0 deletions Cornea Cristian/L03/L03.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Drive.v3" Version="1.55.0.2453" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions Cornea Cristian/L03/L03.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31702.278
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "L03", "L03.csproj", "{2C84BB6A-A07D-4486-B617-E5E02AC1DA76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2C84BB6A-A07D-4486-B617-E5E02AC1DA76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C84BB6A-A07D-4486-B617-E5E02AC1DA76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C84BB6A-A07D-4486-B617-E5E02AC1DA76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C84BB6A-A07D-4486-B617-E5E02AC1DA76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D9706B27-A7AC-4F49-A7EE-E2AD45A77BDE}
EndGlobalSection
EndGlobal
86 changes: 86 additions & 0 deletions Cornea Cristian/L03/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Util.Store;
using Newtonsoft.Json.Linq;

namespace L03
{
class Program
{
private static string _token;

static void Main()
{
Initialize();
}

static void Initialize()
{
string[] scopes = new string[]{
DriveService.Scope.Drive,
DriveService.Scope.DriveFile
};
var clientId = "1046461755374-0jeatkchhepp5kf3t922c30jv09lg0rp.apps.googleusercontent.com";
var clientSecret = "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";
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("Dainto.GoogleDrive.Auth.Store")
).Result;
_token = credential.Token.AccessToken;
Console.WriteLine(_token);
GetAllFiles();
UploadTxtFile();
}

static void GetAllFiles()
{
var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/drive/v3/files?q='root'%20in%20parents");
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _token);
using var response = request.GetResponse();
using Stream data = response.GetResponseStream();
using var reader = new StreamReader(data);
string text = reader.ReadToEnd();
var myData = JObject.Parse(text);
foreach (var file in myData["files"])
{
if (file["mimeType"].ToString() != "application/vnd.google-apps.folder")
{
Console.WriteLine("File name: " + file["name"]);
}
}
}
static void UploadTxtFile()
{
string path;
byte[] text;
Console.Write("Dati calea fisierului txt pe care doriti sa il incarcati pe drive: ");
path = Console.ReadLine();
text = Encoding.ASCII.GetBytes("--not_so_random_boundary\nContent-Type: application/json; charset = utf-8\nContent-Disposition: form-data; name=\"metadata\"\n\n{\"name\":\""+Path.GetFileName(path)+"\",\"mimeType\":\"text/plain\"}\n--not_so_random_boundary\nContent-Type: text/plain\nContent-Disposition: form-data; name=\"file\"\n\n"+ File.ReadAllText(path) + "\n--not_so_random_boundary--");
var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
request.Method = WebRequestMethods.Http.Post;
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _token);
request.Headers.Add(HttpRequestHeader.ContentType, "multipart/related; boundary=not_so_random_boundary");
request.Headers.Add(HttpRequestHeader.ContentLength, text.Length.ToString());
Stream body = request.GetRequestStream();
body.Write(text, 0, text.Length);
using var response = request.GetResponse();
using Stream data = response.GetResponseStream();
using var reader = new StreamReader(data);
string info = reader.ReadToEnd();
var myData = JObject.Parse(info);
Console.WriteLine(myData);
}
}
}
1 change: 1 addition & 0 deletions Cornea Cristian/L03/random.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this text is also random