0
我是一名學生,項目任務是使用C#實現谷歌驅動器到網站。現在我在Visual Studio 2015中測試它。當我從VS啓動網站時,它將啓動一個認證谷歌驅動器。我的身份驗證成功,我可以將文件上傳到雲端硬盤,但auth選項卡未關閉。以下是我的代碼。Google雲端硬盤身份驗證標籤不關閉
Default.aspx.cs
using System;
using System.IO;
using System.Web.UI;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var service = GoogleDriveHelper.AuthenticateOauth();
}
}
GoogleDriveHelper.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
public class GoogleDriveHelper
{
public static DriveService AuthenticateOauth()
{
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile };
try
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets {
ClientId = "My-client-id",
ClientSecret = "My-client-secret"
},
scopes,
"Users",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
DriveService service = new DriveService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "Google Drive API Demo",
});
return service;
}
catch
{
return null;
}
}
這是我的身份驗證選項卡後,成功地驗證谷歌驅動器。問題是,驗證後它沒有關閉。
http://127.0.0.1:54972/authorize/?code=4/mbNAx__wjbnjsH6xJFtsgp3V6bDoweKrI8psJPaZd5E#
因此,這裏是我嘗試解決問題:
- 自動關閉權威性谷歌標籤認證之後。
在IIS中部署此站點時發生另一個錯誤,驗證失敗。
錯誤:redirect_uri_mismatch
謝謝你們提前。
是否需要創建此oauth2callback?或者這是來自谷歌本身? – saf21