3
在C#我想讀取和寫入到谷歌驅動器上的文件,我看到大多數示例應用程序使用控制檯來收集從登錄嘗試返回的參數。我想在一個winforms應用程序中執行此操作。谷歌驅動器讀取和編輯文件,並自動登錄.NET
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;
當談到我想避免的需要,爲用戶複製並粘貼回來代碼的授權,我大量使用谷歌的樣品幫手,但必須有一個更簡單的方法!
私有靜態IAuthorizationState GetAuthorization(NativeApplicationClient客戶端) {
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;
}
什麼我需要做的就是這個工作? 「範圍」是什麼意思?
我讀了無數的文章,它似乎並不容易,沒有人有一個示例winforms應用程序示例>?