Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net OAuth2令牌,消息:';{&&x201C;错误&x201D;:&&"x201C;访问被拒绝”;}&';当我尝试使用OAuth连接到Google日历时返回_Asp.net_Oauth 2.0_Google Calendar Api - Fatal编程技术网

Asp.net OAuth2令牌,消息:';{&&x201C;错误&x201D;:&&"x201C;访问被拒绝”;}&';当我尝试使用OAuth连接到Google日历时返回

Asp.net OAuth2令牌,消息:';{&&x201C;错误&x201D;:&&"x201C;访问被拒绝”;}&';当我尝试使用OAuth连接到Google日历时返回,asp.net,oauth-2.0,google-calendar-api,Asp.net,Oauth 2.0,Google Calendar Api,我使用Google标准库for ASP.NET来使用日历服务版本3,并通过Google API控制台为OAuth 2.0身份验证设置了服务帐户类型。 现在我的目标是使用OAuth2连接日历。因为它对于大多数用户来说工作正常,但是对于一些有限的用户来说却遇到了问题,因为他们得到了错误=访问被拒绝。 我正在使用下面的代码从OAuth2获得身份验证 private static string PrivateFeed = @"https://www.googleapis.com/auth/calend

我使用Google标准库for ASP.NET来使用日历服务版本3,并通过Google API控制台为OAuth 2.0身份验证设置了服务帐户类型。 现在我的目标是使用OAuth2连接日历。因为它对于大多数用户来说工作正常,但是对于一些有限的用户来说却遇到了问题,因为他们得到了错误=访问被拒绝。 我正在使用下面的代码从OAuth2获得身份验证

 private static string PrivateFeed = @"https://www.googleapis.com/auth/calendar";    
public static string GenerateGoogleOAuthURL(String ReturnUrl)
{
    string Url = "https://accounts.google.com/o/oauth2/auth?scope={0}&redirect_uri={1}&response_type={2}&client_id={3}&state={4}&access_type={5}&approval_prompt={6}";
    string scope = UrlEncodeForGoogle(PrivateFeed).Replace("%20", "+");
    string redirect_uri_encode = UrlEncodeForGoogle(ReturnUrl);
    string response_type = "code";
    string state = "";
    string access_type = "offline";
    string approval_prompt = "force";        
    String ClientID = ConfigurationManager.AppSettings["clientID"].ToString();
    return string.Format(Url, scope, redirect_uri_encode, response_type, ClientID, state, access_type, approval_prompt);
}
当返回重定向页面时,我得到一个使用以下代码的刷新令牌:-

 private String ExchangeCodeWithAccessAndRefreshToken()
{
    string Url = "https://accounts.google.com/o/oauth2/token";
    string grant_type = "authorization_code";
    string redirect_uri_encode = string.Empty;
    redirect_uri_encode = UrlEncodeForGoogle(Convert.ToString(Session["URL"]));     
    string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}";
    string Code = Request.QueryString["Code"];
    String ClientID = ConfigurationManager.AppSettings["clientID"].ToString();
    String ClientSecret = ConfigurationManager.AppSettings["clientSecret"].ToString();
    try
    {
        HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
        string result = string.Empty;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";
        string param = string.Format(data, Code, ClientID, ClientSecret, redirect_uri_encode, grant_type);
        var bs = Encoding.UTF8.GetBytes(param);
        using (Stream reqStream = request.GetRequestStream())
        {
            reqStream.Write(bs, 0, bs.Length);
        }
        using (WebResponse response = request.GetResponse())
        {
            var sr = new StreamReader(response.GetResponseStream());
            result = sr.ReadToEnd();
            sr.Close();
        }
        if (!string.IsNullOrEmpty(result))
        {
            var jsonSerializer = new JavaScriptSerializer();
            var tokenData = jsonSerializer.Deserialize<GoogleTokenModel>(result);
            return tokenData.Refresh_Token;
        }
    }
    catch (Exception ex)
    {
    }
    return "";
}
私有字符串ExchangeCodeWithAccessAndRefreshToken()
{
字符串Url=”https://accounts.google.com/o/oauth2/token";
字符串grant\u type=“授权码”;
string redirect\u uri\u encode=string.Empty;
redirect_uri_encode=UrlEncodeForGoogle(Convert.ToString(Session[“URL]”));
string data=“code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}”;
字符串代码=Request.QueryString[“Code”];
字符串ClientID=ConfigurationManager.AppSettings[“ClientID”].ToString();
字符串ClientSecret=ConfigurationManager.AppSettings[“ClientSecret”].ToString();
尝试
{
HttpWebRequest request=HttpWebRequest.Create(Url)为HttpWebRequest;
字符串结果=string.Empty;
request.Method=“POST”;
request.KeepAlive=true;
request.ContentType=“application/x-www-form-urlencoded”;
string param=string.Format(数据、代码、ClientID、ClientSecret、重定向\u uri\u encode、授权类型);
var bs=Encoding.UTF8.GetBytes(参数);
使用(Stream reqStream=request.GetRequestStream())
{
请求流写入(bs,0,bs.长度);
}
使用(WebResponse=request.GetResponse())
{
var sr=新的StreamReader(response.GetResponseStream());
结果=sr.ReadToEnd();
高级关闭();
}
如果(!string.IsNullOrEmpty(结果))
{
var jsonSerializer=新的JavaScriptSerializer();
var tokenData=jsonSerializer.Deserialize(结果);
返回tokenData.Refresh\u令牌;
}
}
捕获(例外情况除外)
{
}
返回“”;
}

我认为我已经将状态指定为空。可能我必须在这里指定一些值。我说的对吗?不对。state参数只是为了方便您将信息从服务传递到oauthcallback。这对授权过程没有影响。谢谢您的回复。你能在我的代码中发现任何问题吗。最重要的是,它对90%的客户有效,只有10%的客户产生了问题。如果它对90%的客户有效,那么问题可能不在您发布的代码中。存储的10%的刷新令牌可能有问题。我在连接calendar means时遇到此问题,而仅进行身份验证。在从上述进程获取刷新令牌后,我将所有刷新令牌存储在DB中。