我让 Google Drive 用户登录并使用他们的 Google Drive 数据。但是,只有第一个用户需要进行身份验证,后续用户才能访问第一个用户的数据!这段代码有什么问题?
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
var path = HttpContext.Current.Server.MapPath(@"~/client_secret.json");
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = HttpContext.Current.Server.MapPath(@"~/token.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
//create Drive API service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
它有效,但帐户绑定到服务器,所有潜在用户都可以访问同一帐户。
客户端库的工作方式是将用户凭据存储在 credpath 中。 每个用户都由您所说的“用户”来表示,
您的代码仅设置一个“用户”,因此每次都会加载单个用户。