【问题标题】:Deploying Gmail Apis Project Authentification probleme部署 Gmail APIs 项目身份验证问题
【发布时间】:2018-12-30 04:32:23
【问题描述】:

当我在本地使用该应用程序时(在使用 Visual Studio 执行时)没有问题,但是当我部署我的项目时,身份验证不起作用。 问题就在这里:

    string path = HostingEnvironment.MapPath("~/App_Data");
    FileDataStore file = new FileDataStore(path);

    credential =new GoogleWebAuthorizationBroker().AuthorizeAsync(
                            new ClientSecrets
                            {
                                ClientId = "Client_ID",
                                ClientSecret = "Client_Secret"
                            },
                            Scopes,
                            "me",
                            CancellationToken.None
                            , file
                            ).Result;

【问题讨论】:

  • 你能分享一下错误信息或错误代码是什么吗?
  • 错误在这里:private bool OpenBrowser(string url) { Process.Start(url);返回真; } 它告诉我访问被拒绝
  • 我在将所有错误日志放入来自 Google.Apis.Auth 项目的文本文件后发现了错误

标签: c# asp.net gmail-api


【解决方案1】:

我终于自己找到了一个解决方案(X 之一),让我们开始吧:

首先我们手动生成授权url,看:

Response.Redirect("https://accounts.google.com/o/oauth2/v2/auth?"+
                "redirect_uri="+ WebConfigurationManager.AppSettings["RedirectUrl"].ToString() + "&" +
                "prompt=consent&"+
                "response_type=code&"+
                "client_id=" + WebConfigurationManager.AppSettings["ClientID"].ToString() + "&" +
                "scope=https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.modify&"+
                "access_type=offline"
                );

之后,我在 web config 上放置的 RedirectUrl 将在身份验证后启动,并在链接中获取一个 Code 作为参数(带有参数 Code 的 URL),所以现在你只需要获取令牌访问权限,所以让我们看看这段代码:

 protected async void Page_Load(object sender, EventArgs e)
            {

                if (Request["code"] != null && Session["credential"] ==null)
                {
                    var result = await getTokenResponse(Request["code"].ToString()); // here we get the code posted by google

                }

            }
private static string[] Scopes = { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailModify };
        async Task<TokenResponse> getTokenResponse(String Code)
        {
            Code = Code.Replace("#", "");
            string redirectUri = WebConfigurationManager.AppSettings["RedirectUrl"].ToString();
            var init2 = new GoogleAuthorizationCodeFlow.Initializer();
            ClientSecrets cli = new ClientSecrets(); 
            cli.ClientId = WebConfigurationManager.AppSettings["ClientID"].ToString(); // init the Client_ID
            cli.ClientSecret = "ClientSecret"; // init the Client_Secret
            init2.ClientSecrets = cli;
            init2.Scopes = Scopes;
            init2.DataStore = null; // you dont need to store token cause we w'll store it in Session object
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(init2); /// init the flow which w'll get the token object 
            CancellationToken cancellationToken = default(CancellationToken);
            var token = await flow.ExchangeCodeForTokenAsync("me", Code, redirectUri, cancellationToken); 
            Response.Write(token);

            UserCredential credential = new UserCredential(flow, "me", token); // and know we have the credential 
            Session["credential"] = credential;
            return token;
        }

注意:

  1. 此解决方案适用于多客户端应用程序。
  2. 对于所有使用 google api 的请求,我们有:只是处理程序返回的文本或 Json 文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2013-03-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    相关资源
    最近更新 更多