Box api Box API在获取访问令牌时始终返回无效的grant_类型参数
我正在为WP8编写自己的Box SDK,以充分利用任务。我在获取访问令牌时遇到问题。我总是以此作为回报:Box api Box API在获取访问令牌时始终返回无效的grant_类型参数,box-api,Box Api,我正在为WP8编写自己的Box SDK,以充分利用任务。我在获取访问令牌时遇到问题。我总是以此作为回报: {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"} 我使用的代码(全部为C#)是: internal const String TokenURL = "https://api.box.com/oauth2/token"; CloudH
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
我使用的代码(全部为C#)是:
internal const String TokenURL = "https://api.box.com/oauth2/token";
CloudHttpAsync.DownloadResponceStreamAsync
(
CloudHttpAsync.PostAsync
(
TokenURL,
new MemoryStream
(
UTF8Encoding.UTF8.GetBytes
(
HttpUtility.UrlEncode
(
String.Format
(
"grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}&redirect_uri={3}",
Code,
ClientID,
ClientSecret,
RedirectURI
)
)
)
),
null,
null
),
null
).ContinueWith((AsyncStream) =>
{
try
{
if (AsyncStream.Exception != null)
{
TaskSource.TrySetException(AsyncStream.Exception.InnerExceptions);
return;
}
String Result = "";
using (StreamReader Reader = new StreamReader(AsyncStream.Result))
{
Result = Reader.ReadToEnd();
}
BoxAuthToken Token = JsonConvert.DeserializeObject<BoxAuthToken>(Result);
TaskSource.TrySetResult(Token);
}
catch (Exception e)
{
TaskSource.TrySetException(e);
}
});
返回:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 01 Mar 2013 07:35:22 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=51305a3a187f34.52738262; expires=Sat, 01-Mar-2014 07:35:22 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Tue, 30-Apr-2013 07:35:22 GMT; path=/
Cache-Control: no-store
Content-Length: 99
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 01 Mar 2013 07:50:31 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=51305dc751d7f5.67064854; expires=Sat, 01-Mar-2014 07:50:31 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Tue, 30-Apr-2013 07:50:31 GMT; path=/
Cache-Control: no-store
Content-Length: 99
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
URL对整个字符串进行编码时:
POST https://api.box.com/oauth2/token HTTP/1.1
Accept: */*
Content-Length: 214
Accept-Encoding: identity
User-Agent: NativeHost
Host: api.box.com
Connection: Keep-Alive
Cache-Control: no-cache
grant_type%3dauthorization_code%26code%3d3ikruv5elfdw3fOP55aMDSX7ybLqBFlA%26client_id%3d[subbed]%26client_secret%3d[subbed]%26redirect_uri%3dhttps%3a%2f%2fCloudBoxWP8
返回
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 01 Mar 2013 07:46:03 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=51305cbb339de4.03221876; expires=Sat, 01-Mar-2014 07:46:03 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Tue, 30-Apr-2013 07:46:03 GMT; path=/
Cache-Control: no-store
Content-Length: 99
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
无URL编码:
POST https://api.box.com/oauth2/token HTTP/1.1
Accept: */*
Content-Length: 190
Accept-Encoding: identity
User-Agent: NativeHost
Host: api.box.com
Connection: Keep-Alive
Cache-Control: no-cache
grant_type=authorization_code&code=2wgIzfqhvIgRtVIp2ZvqZ9X8R5u0QNaf&client_id=[subbed]&client_secret=[subbed]&redirect_uri=https://CloudBoxWP8
返回:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 01 Mar 2013 07:35:22 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=51305a3a187f34.52738262; expires=Sat, 01-Mar-2014 07:35:22 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Tue, 30-Apr-2013 07:35:22 GMT; path=/
Cache-Control: no-store
Content-Length: 99
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 01 Mar 2013 07:50:31 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=51305dc751d7f5.67064854; expires=Sat, 01-Mar-2014 07:50:31 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Tue, 30-Apr-2013 07:50:31 GMT; path=/
Cache-Control: no-store
Content-Length: 99
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
请求/响应会有所帮助。看起来您正在对整个查询字符串而不是每个值进行URL编码。将作为以下文件提交给我们: 授予类型%3Dauthorization\u代码%26代码%3Dxyz%26客户端\u id%3Dxyz%26客户端\u机密%3Dxyz%26重定向\u uri%3Dxyz 而不是:
grant\u type=authorization\u code&code=xyz&client\u id=xyz&client\u secret=xyz&redirect\u uri=xyz我认为在请求正文中包含
redirect\u uri
可能会使事情变得复杂,特别是因为它看起来被设置为无效值(https://CloudBoxWP8
)您可以通过将应用程序设置为处理自定义协议(cloudboxwp8://
)并预配置框以在授予令牌时重定向到该协议来解决此问题
cloudboxwp8
李>
cloudboxwp8://tokengrated
。在这里实现您的令牌处理逻辑重定向\u uri
字段中,从步骤2开始将值设置为自定义协议和端点。保存您的更改重定向\u uri
,然后重试请求Box API文档中没有列出它,但是检索访问令牌的请求需要标题
内容类型:application/x-www-form-urlencoded
在StackOverflow上找到答案之前,我也在这一部分停留了一段时间。但我忘记了它的链接。在Windows phone 8.1 WinRT中
Dictionary<string, string> contentList = new Dictionary<string, string>();
contentList.Add("code", code);
contentList.Add("client_id", client_id);
contentList.Add("client_secret", clientSecret);
contentList.Add("redirect_uri", redirectUri);
contentList.Add("grant_type", "authorization_code");
FormUrlEncodedContent content = new FormUrlEncodedContent(contentList);
var response = await client.PostAsync(baseURL, content);
YouTubeAutenticationResponse res = JsonConvert.DeserializeObject<YouTubeAutenticationResponse>(await response.Content.ReadAsStringAsync());
public class YouTubeAutenticationResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("expires_in")]
public string ExpiresIn { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
}
Dictionary contentList=new Dictionary();
内容列表。添加(“代码”,代码);
contentList.Add(“client\u id”,client\u id);
contentList.Add(“client\u secret”,clientSecret);
添加(“重定向uri”,重定向uri);
添加(“授权类型”、“授权代码”);
FormUrlEncodedContent内容=新的FormUrlEncodedContent(内容列表);
var response=wait client.PostAsync(baseURL,content);
youtubeautensionresponse=JsonConvert.DeserializeObject(wait response.Content.ReadAsStringAsync());
公共类YouTubeauthenticationResponse
{
[JsonProperty(“访问令牌”)]
公共字符串AccessToken{get;set;}
[JsonProperty(“令牌类型”)]
公共字符串标记类型{get;set;}
[JsonProperty(“到期日”)]
公共字符串过期在{get;set;}
[JsonProperty(“刷新令牌”)]
公共字符串刷新标记{get;set;}
}
Tom,您可以发布发送到Box的HTTP请求的跟踪信息吗?另外,您是否对重定向uri进行了URL编码?只是尝试了将重定向地址的URL编码为相同的结果。现在使用fiddler,我尝试使用它发送帖子,使用应该是有效的请求主体,它还返回no grant_类型的错误。再试一次,我用请求主体“grant\u type=authorization\u code”向发送了一个POST请求,但仍然得到了no grant\u type错误!您可以发布原始请求和响应数据(保留任何机密信息)吗?完成,希望这有助于我们不要尝试这样做。我已经发布了原始的请求和响应数据,这样你就可以看到你自己了。过去几天我一直有点忙,到周末都会很忙。那我就试试这个。只需指出,我可以通过应用程序中弹出的WebBrowser控件(因此我正在编写的库是完全独立的)很好地执行登录部分,该方法与其他OAuth实现(如Dropbox)配合使用也很好。很抱歉迟了回复!