嗨朋友我第一次使用google api进行身份验证,并使用asp.net将文件上传到谷歌电子表格。任何人都可以帮我解决错误:
错误1类型'Google.Apis.Http.IHttpUnsuccessfulResponseHandler' 在未引用的程序集中定义。你必须添加一个 参考汇编'Google.Apis,版本= 1.6.0.16897, 文化=中性, 公钥=空”。
错误2“Google.Apis.Authentication.IAuthenticator”类型是 在未引用的程序集中定义。你必须添加一个 参考汇编'Google.Apis,版本= 1.6.0.16897, 文化=中性, 公钥=空”。
错误3'Google.Apis.Services.BaseClientService.Initializer'没有 包含的定义 '认证者'
错误4'Google.Apis.Drive.v2.DriveService'不包含 的定义 '范围'
我的代码如下: -
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Services;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadFile(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Session["File"] = FileUpload1.PostedFile;
Session["Description"] = txtDescription.Text;
String CLIENT_ID = "YOUR_CLIENT_ID";
String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
// 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;
Console.WriteLine("File id: " + file.Id);
Console.WriteLine("Press Enter to end this process.");
Console.ReadLine();
}
}
public 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);
}
}