C# Google drive读取和编辑文件,并自动登录.NET
在c#中,我想读写google drive上的文件,我看到大多数示例应用程序使用控制台收集登录尝试返回的参数。我想在winforms应用程序中执行此操作C# Google drive读取和编辑文件,并自动登录.NET,c#,winforms,google-drive-api,C#,Winforms,Google Drive Api,在c#中,我想读写google drive上的文件,我看到大多数示例应用程序使用控制台收集登录尝试返回的参数。我想在winforms应用程序中执行此操作 String CLIENT_ID = "XXXX"; String CLIENT_SECRET = "XXXX"; // Register the authenticator and create the service var provider = new NativeAppl
String CLIENT_ID = "XXXX";
String CLIENT_SECRET = "XXXX";
// Register the authenticator and create the service
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
var service = new DriveService(new BaseClientService.Initializer()
{
Authenticator = auth
});
File body = new File();
body.Title = "My document";
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
我需要做什么才能让它工作?“范围”是指什么
我读了无数的文章,这似乎并不容易,有人有winforms app example>的例子吗?真是个笨蛋
字符串范围=”
上面的代码是有效的-只需将其归咎于凌晨3点的编码
const string STORAGE = "XXXX";
const string KEY = "XXXX";
string scope = "";
// Check if there is a cached refresh token available.
IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
if (state != null)
{
try
{
client.RefreshToken(state);
return state; // Yes - we are done.
}
catch (DotNetOpenAuth.Messaging.ProtocolException ex)
{
//CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
}
}
// Retrieve the authorization from the user.
state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
return state;
}