Asp.net mvc 如何在没有身份验证提示的情况下在控制台中获取oauth访问令牌
我想要像这样的身份验证 但我不想让你知道 我想不弹出窗口直接获取令牌Asp.net mvc 如何在没有身份验证提示的情况下在控制台中获取oauth访问令牌,asp.net-mvc,c#-4.0,google-api,google-oauth,Asp.net Mvc,C# 4.0,Google Api,Google Oauth,我想要像这样的身份验证 但我不想让你知道 我想不弹出窗口直接获取令牌 public ActionResult CodeLele() { if (Session.Contents.Count > 0) { if (Session["loginWith"] != null) { if (Session["loginWith"].ToString() == "google")
public ActionResult CodeLele()
{
if (Session.Contents.Count > 0)
{
if (Session["loginWith"] != null)
{
if (Session["loginWith"].ToString() == "google")
{
try
{
var url = Request.Url.Query;
if (url != "")
{
string queryString = url.ToString();
char[] delimiterChars = { '=' };
string[] words = queryString.Split(delimiterChars);
string code = words[1];
if (code != null)
{
//get the access token
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
webRequest.Method = "POST";
Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_sceret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
Stream postStream = webRequest.GetRequestStream();
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
WebResponse response = webRequest.GetResponse();
postStream = response.GetResponseStream();
StreamReader reader = new StreamReader(postStream);
string responseFromServer = reader.ReadToEnd();
GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);
if (serStatus != null)
{
string accessToken = string.Empty;
accessToken = serStatus.access_token;
if (!string.IsNullOrEmpty(accessToken))
{
// This is where you want to add the code if login is successful.
// getgoogleplususerdataSer(accessToken);
}
else
{ }
}
else
{ }
}
else
{ }
}
}
catch (WebException ex)
{
try
{
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
//var messageFromServer = obj.error.message;
//return messageFromServer;
return obj.error_description;
}
catch (Exception exc)
{
throw exc;
}
}
}
}
}
return Content("done");
}
public ActionResult JeClick()
{
var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" + googleplus_redirect_url + "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" + googleplus_client_id;
Session["loginWith"] = "google";
return Redirect(Googleurl);
}
public ActionResult CodeLele()
{
如果(Session.Contents.Count>0)
{
如果(会话[“loginWith”]!=null)
{
如果(会话[“loginWith”].ToString()=“google”)
{
尝试
{
var url=Request.url.Query;
如果(url!=“”)
{
字符串queryString=url.ToString();
char[]delimiterChars={'='};
string[]words=queryString.Split(delimiterChars);
字符串代码=单词[1];
如果(代码!=null)
{
//获取访问令牌
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(“https://accounts.google.com/o/oauth2/token");
webRequest.Method=“POST”;
Parameters=“code=“+code+”&client\u id=“+googleplus\u client\u id+”&client\u secret=“+googleplus\u client\u sceret+”&redirect\u uri=“+googleplus\u redirect\u url+”&grant\u type=authorization\u code”;
byte[]byteArray=Encoding.UTF8.GetBytes(参数);
webRequest.ContentType=“application/x-www-form-urlencoded”;
webRequest.ContentLength=byteArray.Length;
Stream postStream=webRequest.GetRequestStream();
//将post数据添加到web请求
Write(byteArray,0,byteArray.Length);
postStream.Close();
WebResponse=webRequest.GetResponse();
postStream=response.GetResponseStream();
StreamReader=新的StreamReader(postStream);
字符串responseFromServer=reader.ReadToEnd();
GooglePlusAccessToken serStatus=JsonConvert.DeserializeObject(responseFromServer);
if(serStatus!=null)
{
string accessToken=string.Empty;
accessToken=serStatus.access\u令牌;
如果(!string.IsNullOrEmpty(accessToken))
{
//如果登录成功,您希望在此处添加代码。
//getgoogleplususerdataSer(accessToken);
}
其他的
{ }
}
其他的
{ }
}
其他的
{ }
}
}
捕获(WebException ex)
{
尝试
{
var resp=newstreamreader(例如Response.GetResponseStream()).ReadToEnd();
动态对象=JsonConvert.DeserializeObject(resp);
//var messageFromServer=obj.error.message;
//从服务器返回消息;
返回obj.error\u说明;
}
捕获(异常exc)
{
抛出exc;
}
}
}
}
}
返回内容(“完成”);
}
公共行动结果点击()
{
var Googleurl=”https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=“+googleplus\u重定向\u url+”&范围=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=“+googleplus_客户id;
会话[“loginWith”]=“google”;
返回重定向(Googleurl);
}
凭证窗口(弹出窗口)是询问用户是否可以访问其数据的方式。如果不先询问用户是否可以访问他们的数据,就无法访问用户的数据。这就是Oauth2的工作原理
如果您正在访问自己的数据,那么您可以使用称为服务帐户的东西。是预先授权的。您可以使用该服务帐户并授予它访问您的google日历的权限,您可以授予它访问google drive中某个文件夹的权限。然后,您可以使用服务帐户进行身份验证。服务帐户就像虚拟用户
我关于服务帐户的文章:凭证窗口(弹出窗口)是询问用户是否可以访问他们的数据的方式。如果不先询问用户是否可以访问他们的数据,就无法访问用户的数据。这就是Oauth2的工作原理
如果您正在访问自己的数据,那么您可以使用称为服务帐户的东西。是预先授权的。您可以使用该服务帐户并授予它访问您的google日历的权限,您可以授予它访问google drive中某个文件夹的权限。然后,您可以使用服务帐户进行身份验证。服务帐户就像虚拟用户
我关于服务帐户的文章:但这是我自己的帐户,我想通过它访问web store Api,我只需要访问令牌,它需要一个代码,该代码将随重定向URI一起发送,然后根据您将要使用的Api,您应该查看一个使用服务帐户的帐户。我尝试了很多次尝试,但是