部署Gmail Apis项目身份验证问题 [英] Deploying Gmail Apis Project Authentification probleme

查看:43
本文介绍了部署Gmail Apis项目身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地使用该应用程序(在Visual Studio中执行)时,没有问题,但是当我部署我的项目时,身份验证不起作用. 问题位于此处:

when i use the app in local (on executing with visual studio) there is no probleme, but when i deploy my project the authentification doesn't work. the probleme is situated here :

    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;

推荐答案

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

Finally I have found a solution (one of X) by my self so let begin:

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

First we generate the authorization url manually, look:

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),所以现在您只需要获得令牌访问权限,因此让我们看一下这段代码:

after that the RedirectUrl which I put on web config will be start after authentication and get a Code as parameter in the link (url with parameter Code), so now you just need to get token access, so lets see this code:

 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文本.

这篇关于部署Gmail Apis项目身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆