VAR*_*RAK 4 c# webrequest query-string
我正在尝试按照以下说明调用Google的OAuth2身份验证服务:https://developers.google.com/accounts/docs/OAuth2ForDevices
我将所有必需的参数放入查询字符串并发送请求.这适用于"获取用户代码"部分,但不适用于"获取访问和刷新令牌"部分.
经过多次游戏并获得400 Bad Request错误后,我发现,您可以使用FormUrlEncodedContent创建请求,而不是将数据放入查询字符串中,并通过application\x-www-form-urlencoded将内容作为内容发送内容类型.
这是以前的代码:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(fullUrl);
Run Code Online (Sandbox Code Playgroud)
其中fullUrl是这样的:
https://accounts.google.com/o/oauth2/device/code?client_id=812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
Run Code Online (Sandbox Code Playgroud)
新代码是:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(url);
requestMessage.Content = new FormUrlEncodedContent(CreateDictionary(queryStringNames, queryStringValues));
Run Code Online (Sandbox Code Playgroud)
网址是:
https://accounts.google.com/o/oauth2/device/code
Run Code Online (Sandbox Code Playgroud)
queryStringNames和queryStringValues是所需参数的名称和值的字符串数组.
这两种方法有什么区别?是否可以安全地假设所有POST调用都可以使用URL编码内容请求而不是将数据放入查询字符串中?
通常,POST请求不需要查询字符串,但它仍然受到服务器逻辑实现的影响.如果OAuth是众所周知的标准并且它们遵循良好实践,则使用表单编码数据是安全的,除非在API中明确提到将参数作为查询字符串发送.
查询字符串和发布数据是两组不同的参数.如果服务器需要查询字符串,则必须仅发送查询字符串.这完全取决于服务器端逻辑的实现方式.你不能交替使用它们.大多数API文档明确指出了他们期望的内容.
| 归档时间: |
|
| 查看次数: |
2166 次 |
| 最近记录: |