C# 部署Gmail API项目身份验证问题

C# 部署Gmail API项目身份验证问题,c#,asp.net,gmail-api,C#,Asp.net,Gmail Api,当我在本地使用应用程序时(使用visual studio执行时),没有问题,但是当我部署项目时,身份验证不起作用。 问题就在这里: string path = HostingEnvironment.MapPath("~/App_Data"); FileDataStore file = new FileDataStore(path); credential =new GoogleWebAuthorizationBroker().AuthorizeAsync(

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

最后,我自己找到了一个解决方案(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配置上的重定向url将在身份验证后启动,并在链接中获得一个代码作为参数(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;
        }
protected async void Page\u加载(对象发送方,事件参数e)
{
如果(请求[“代码”]!=null和会话[“凭证”]==null)
{
var result=await getTokenResponse(Request[“code”].ToString());//这里是google发布的代码
}
}
私有静态字符串[]Scopes={GmailService.Scope.GmailReadonly,GmailService.Scope.GmailModify};
异步任务getTokenResponse(字符串代码)
{
代码=代码。替换(“#”和“”);
string redirectUri=WebConfiguration Manager.AppSettings[“RedirectUrl”].ToString();
var init2=新的GoogleAuthorizationCodeFlow.Initializer();
ClientSecrets cli=新的ClientSecrets();
cli.ClientId=WebConfigurationManager.AppSettings[“ClientId”].ToString();//初始化客户端ID
ClientSecret=“ClientSecret”;//初始化客户端\u密码
init2.ClientSecrets=cli;
init2.Scopes=Scopes;
init2.DataStore=null;//您不需要存储令牌,因为我们将它存储在会话对象中
GoogleAuthorizationCodeFlow=新的GoogleAuthorizationCodeFlow(init2);///初始化将获取令牌对象的流
CancellationToken CancellationToken=默认值(CancellationToken);
var token=wait flow.ExchangeCodeForTokenAsync(“me”,代码,重定向URI,取消令牌);
响应。写入(令牌);
UserCredential=新的UserCredential(流,“我”,令牌);//并且知道我们有凭证
会话[“凭证”]=凭证;
返回令牌;
}
注:

  • 此解决方案适用于多客户端应用程序
  • 对于使用GoogleAPI的所有请求,我们只有:处理程序返回的文本或Json文本

  • 你能告诉我什么是错误消息或错误代码吗?错误在这里:private bool OpenBrowser(string url){Process.Start(url);return true;}它告诉我access deniedi将所有错误日志放在一个文本文件中,从Google.api.Auth项目中找到了错误