5
Có ai có ví dụ đơn giản về cách sử dụng Google.Apis.Auth.OAuth với C# Webforms không? Tôi chỉ có thể tìm thấy những cái MVC.Google.Apis.Auth.OAuth cho biểu mẫu web
Có ai có ví dụ đơn giản về cách sử dụng Google.Apis.Auth.OAuth với C# Webforms không? Tôi chỉ có thể tìm thấy những cái MVC.Google.Apis.Auth.OAuth cho biểu mẫu web
Nó được một lúc nhưng tôi nghĩ rằng điều này có thể có ích cho ai đó:
GoogleFlowMetaData:
public class GoogleFlowMetaData
{
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "your client ID",
ClientSecret = "your client secret"
},
Scopes = new[] { CalendarService.Scope.Drive/*or any service you want*/ },
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
public IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
AuthorizationCodeApp:
public class AuthorizationCodeApp : AuthorizationCodeWebApp
{
private readonly GoogleFlowMetaData flowData;
private readonly string redirectUri;
private readonly string state;
private readonly string userID;
public GoogleFlowMetaData FlowData { get { return flowData; } }
public AuthorizationCodeApp(GoogleFlowMetaData flowData, string redirectUri, string state, string userID):base(
flowData.Flow,redirectUri,state)
{
this.redirectUri = redirectUri;
this.state = state;
this.userID = userID;
}
public Task<AuthResult> AuthorizeAsync(CancellationToken taskCancellationToken)
{
return base.AuthorizeAsync(userID, taskCancellationToken);
}
}
tại để chạy một nhiệm vụ Async bất cứ nơi nào bạn có thể muốn để xác định một cái gì đó như:
public async Task<string> IndexAsync(CancellationToken cancellationToken)
{
var result = await new AuthorizationCodeApp(new GoogleFlowMetaData(), "http://localhost:4356/API/GAuth","","beebee").
AuthorizeAsync(cancellationToken);
if (result.Credential != null)
{
var service = new DriveService(new BaseClientService.Initializer
{
// YOUR CODE SHOULD BE HERE..
});
}
else
{
return result.RedirectUri;
}
}
để gọi hàm trên sử dụng không đồng bộ một cái gì đó như:
var cancelToken = new CancellationTokenSource();
var z = Task.Factory.StartNew(() => IndexAsync(cancelToken.Token));