Javascript 谷歌驱动API认证-在客户端进行认证?

Javascript 谷歌驱动API认证-在客户端进行认证?,javascript,asp.net,authentication,google-drive-api,Javascript,Asp.net,Authentication,Google Drive Api,当用户希望通过我的服务访问他们的google drive时,我在我的后端调用以下代码 public static DriveService GetService(string app_userID) { //get Credentials from client_secret.json file UserCredential credential; var path = HttpContext.Current.Server.MapP

当用户希望通过我的服务访问他们的google drive时,我在我的后端调用以下代码

  public static DriveService GetService(string app_userID)
    {

        //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,
                app_userID,
                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;
但问题是,我认为这段代码是为在前端应用程序上运行而开发的。我得到以下错误,我认为程序试图打开一个网页进行身份验证,但在后端服务器上

{
"Message": "An error has occurred.",
"ExceptionMessage": "One or more errors occurred.",
"ExceptionType": "System.AggregateException",
"StackTrace": "   at System.Threading.Tasks.Task`1.GetResultCore(Boolean .......GoogleDrive.GoogleDriveFilesRepository.GetService(String tt_userID) in ,,,
"InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Failed to launch browser with \"...(google url)..." for authorization. See inner exception for details.",
    "ExceptionType": "System.NotSupportedException",
    "StackTrace": "   ...
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Access is denied",
        "ExceptionType": "System.ComponentModel.Win32Exception",
        "StackTrace": "   at ...
如何更改,以便最终用户在客户端进行身份验证?请参阅下面的设置


请注意,如果可能的话,我仍然希望能够从后端调用API。

好吧,这就是我最终解决问题的方法

身份验证必须在客户端完成,因此也可以在java脚本代码中实现。身份验证将产生身份验证代码

由于我是在vue中实现的,所以我使用了以下库:

如果您想直接在javascript中实现,可能会有所帮助(第一部分直到您获得“代码”)

身份验证代码可用于请求访问令牌一次。身份验证代码需要通过使用ASP.NET中实现的API传递到后端服务器

在后端方面,我与一起使用。唯一的例外是我将重定向uri设置为“postmessage”。通过这种方法,我可以在不使用重定向uri的情况下获取访问令牌。不确定重定向uri的目的。但是,使用“postmessage”时,不需要在Google控制台中设置重定向uri

我在服务器和web客户端上都使用了相同的clientID。它工作得很好,但我不确定它是否应该是这样的


最后,为了能够实现oauth2,您需要在一定程度上理解oauth2。下图给出了一个很好的概述。但是,如果您计划在实际的生产环境中实现此功能,则应确保您掌握了此主题,或得到了相关人员的帮助。

这对您有帮助吗?是的,可能是这样,我将尝试使用身份验证部分,然后将访问令牌发送到后端,并使用如下所述的is:您好,您的问题解决了吗?如果是这样,你能把答案贴出来帮助更多的人吗。如果没有,你能更新这个问题吗?嗨,我还在研究这个问题。当我有答案时,我会更新。答案现在发布。你能将你的答案标记为cossrect以便更多的人可以从中受益吗?见: