部署Gmail Apis项目身份验证问题

时间:2018-07-22 15:37:17

标签: c# asp.net gmail-api

当我在本地使用该应用程序时(在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;

1 个答案:

答案 0 :(得分:0)

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

首先,我们手动生成授权网址,如下:

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配置上的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. 对于使用api的所有请求,我们都有:仅仅是处理程序返回的文本或Json文本。