asp.net 4.0 Web アプリケーションを作成しています。ClientID と Client Secret を作成しました。ユーザーを認証し、Web アプリケーションから Google ドライブにファイルをアップロードしたいと考えています。このリンクを確認しました:- https://developers.google.com/drive/quickstart-cs。
しかし、それはコンソールアプリケーションに関連しています。ASP.net Web アプリケーションを使用してファイルをアップロードするにはどうすればよいですか?
ここにコードサンプルがあります
protected void btnSubmit_Click(object sender, EventArgs e)
{String CLIENT_ID = "XXXXX";
String CLIENT_SECRET = "YYYYY";
// 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";
//my code start
//my code end
byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("~/GoogleDrive/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;
Response.Write("File id: " + file.Id);
Response.Write("Press Enter to end this process.");}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write("Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
前もって感謝します!!