我在将 Google Drive 与我的应用程序连接时遇到了同样的问题,但现在我找到了一个绝对适合我的解决方案。 Google 客户端库将为您处理所有这些。下载 nuget 包 Google.GData.Client 和 Google.GData.Documents。
按照我的代码
parameters = new OAuth2Parameters()
{
ClientId = "CLIENT_ID",
ClientSecret = "CLIENT_SECRET",
RedirectUri = currentURL,//"http://localhost:6600/Home.html",
Scope = "https://docs.google.com/feeds/ ",
State = "documents",
AccessType = "offline", // offline means it creats a refreshtoken
TokenExpiry = DateTime.Now.AddYears(1)
};
string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
1) 此处用户应使用他的帐户登录,以便将您的页面重定向到此处
2)当你回到你的页面时(Oauth Redirect Page)
3) 然后从 url(QueryString) 获取代码和状态
4)发送到服务器(页面加载事件)
5)编写以下代码page_load事件(首先在代码变量中获取查询字符串)
OAuth2Parameters parameters = new OAuth2Parameters()
{
ClientId = "274488228041-1aaq8a069h3c7lsjstsl394725tumdlo.apps.googleusercontent.com",
ClientSecret = "Ew1EMwe4EB8oLHvKFfDZxQhp",
RedirectUri = currentURL,//"http://localhost:6600/Home.html"
Scope = "https://docs.google.com/feeds/ ",
State = "documents",
AccessType = "offline", // offline means it creats a refreshtoken
TokenExpiry = DateTime.Now.AddYears(1)
};
parameters.AccessCode = code;
Google.GData.Client.OAuthUtil.GetAccessToken(parameters);
1) 在这里你会得到 accesstoken 和 requesttoken
2)将其保存在数据库中以备将来使用(离线访问)
3) 传递参数以访问 Google Drive Documents
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "Infactum", parameters);
DocumentsService service = new DocumentsService("Infactum");
service.RequestFactory = requestFactory;
//requestFactory.CustomHeaders["Authorization"] = "Bearer " + parameters.AccessToken;
DocumentsListQuery query = new DocumentsListQuery();
query.NumberToRetrieve = 2000;
// Make a request to the API and get all documents.
DocumentsFeed feed = service.Query(query);
在这里,您将在 feed 对象中获得所有类型的 2000 个文件,您可以使用 feed.entries 访问文件...希望您喜欢它