【发布时间】:2015-09-10 13:43:36
【问题描述】:
我在发出检索访问令牌的请求时遇到以下问题。首先,我在开发者控制台中注册了应用程序,然后下载了客户端密钥文件。其内容如下:(我已将机密标记为xxxxx)。
{"installed":{"client_id":"xxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxx","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
然而,在开发者文档(位于:https://developers.google.com/identity/protocols/OAuth2InstalledApp)中,它被赋予了不同的地址来连接和检索访问令牌。
POST /oauth2/v3/token HTTP/1.1 主机:www.googleapis.com 内容类型:application/x-www-form-urlencoded
我很困惑 1. 使用哪个 URI 来访问令牌。 2. 应该使用什么redirect_uri?它是本地主机还是开发人员文档中所述的 uri。
当我使用客户端秘密令牌 uri 时,我收到 400 错误请求,当我按照开发者文档中的说明使用 uri 时,我收到禁止 403。
POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
有人可以澄清一下吗?这将是一个巨大的帮助。 我正在编写一个控制台应用程序,我不想使用已经提供的 C# api。示例代码位于下方。
我哪里做错了?
string tokenUri = @"https://accounts.google.com/o/oauth2/token";
HttpWebRequest request=(HttpWebRequest) WebRequest.Create(tokenUri);
NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("code", this.clientCode);
outgoingQueryString.Add("client_id", this.clientID);
outgoingQueryString.Add("client_secret", this.clientSecret);
outgoingQueryString.Add("redirect_uri", "https://oauth2-login-demo.appspot.com/code");
outgoingQueryString.Add("grant_type","authorization_code");
string postdata = outgoingQueryString.ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream reqStr = request.GetRequestStream();
reqStr.Write(byteArray, 0, byteArray.Length);
reqStr.Flush();
reqStr.Close();
HttpWebResponse response=request.GetResponse() as HttpWebResponse;
Console.WriteLine(response.StatusCode.ToString());
发现不使用 url-encoded,而是使用 json。修改代码如下,仍然 400 持续存在。
string tokenUri = "https://accounts.google.com/o/oauth2/token";
TokenFileds f = new TokenFileds() { client_code = this.clientCode, client_id = this.clientID, client_secret = this.clientSecret, redirect_uri = "urn:ietf:wg:oauth:2.0:oob", grant_type = "authorization_code" };
//string retString=this.SerializeToJson<TokenFileds>(f);
string retString = this.NewjsonLib(f);
byte[] byteArray=Encoding.UTF8.GetBytes(retString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tokenUri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
request.ContentLength = byteArray.Length;
Stream strm = request.GetRequestStream();
strm.Write(byteArray, 0, byteArray.Length);
strm.Flush();
strm.Close();
HttpWebResponse response =request.GetResponse() as HttpWebResponse;
【问题讨论】:
-
这可能对daimto.com/google-3-legged-oauth2-flow 有所帮助,但您有什么理由这样做而不是使用 Google 的客户端库? nuget.org/packages/Google.Apis.Drive.v2
-
我想开发自己的库。原因是,我不喜欢他们编写代码接口的方式。在您发布的链接中,是代码......网址或帖子正文的一部分。我从文档中假设它是 url 编码的正文。
-
检查您发布的 json 数据。这不是您在代码中发送的重定向 uri,它们必须相同。
-
我已根据链接将重定向 uri 更改为“urn:ietf:wg:oauth:2.0:oob”。另外,我发现数据不是url编码的,它必须是json。因此,使用 newtonsoft.json 重新打包代码,生成以下输出。 “{\” client_code \ “:\” 4 / sZ7URBc5qJ6vYt5t3dLz_xyZH-R-ptZdxCZTkx9hdYo \ “\ ”CLIENT_ID \“:\ ”755933294823-t0afvat7k75c49io03o8c0noer7m17m9.apps.googleusercontent.com \“,\ ”client_secret \“:\” 9e1JLC1Tawra4xZwCTHxxT2h \ ",\"redirect_uri\":\"urn:ietf:wg:oauth:2.0:oob\",\"grant_type\":\"authorization_code\"}"。但仍然是同样的问题。 @DaImTo。 400 错误请求。我错过了什么吗?
-
您没有使用 json 文件,您是否正在以艰难的方式执行此操作。阅读我发给你的第一个链接,它将引导你了解每个请求需要看起来像 daimto.com/google-3-legged-oauth2-flow 如果你在我的网站上做的时间足够长,你可能会找到一篇关于如何做这个 IC 的帖子#我有一篇关于这个地方。
标签: c# google-drive-api httpwebrequest