``我正在创建 asp.net 4.0 Web 应用程序。我已经创建了 ClientID 和 Client Secret 。我想从我的网络应用程序验证用户并在谷歌驱动器中上传文件。我已经检查了这个链接:- 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);
}
提前致谢!!