1生成Oauth_Nonce
新浪的Oauth_Nonce是ASCII编码 所有随机生成的时候是只要数字就行 而OAuth标准好像是可以有字母大小写
2中文编码
中文采用Uri.EscapeDataString进行编码的话能够避免+号的问题
(至于具体怎么回事看这个http://www.cnblogs.com/guangrou/archive/2011/02/25/1965294.html )
这里提供两个编码的方法
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value"></param>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
public static string UrlEncodeStrict(string value)
{
// 忽略掉%否则签名会出错
var original = value;
var ret = original.Where(
c => !Unreserved.Contains(c) && c != '%').Aggregate(
value, (current, c) => current.Replace(
c.ToString(), c.ToString().PercentEncode()
));
return ret.Replace("%%", "%25%"); // Revisit to encode actual %'s
}
public static string PercentEncode(this string s)
{
var bytes = s.GetBytes();
var sb = new StringBuilder();
foreach (var b in bytes)
{
// [DC]: Support proper encoding of special characters (\n\r\t\b)
if((b > 7 && b < 11) || b == 13)
{
sb.Append(string.Format("%0{0:X}", b));
}
else
{
sb.Append(string.Format("%{0:X}", b));
}
}
return sb.ToString();
}
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value"></param>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
public static string UrlEncodeRelaxed(string value)
{
return Uri.EscapeDataString(value);
}
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value"></param>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
public static string UrlEncodeStrict(string value)
{
// 忽略掉%否则签名会出错
var original = value;
var ret = original.Where(
c => !Unreserved.Contains(c) && c != '%').Aggregate(
value, (current, c) => current.Replace(
c.ToString(), c.ToString().PercentEncode()
));
return ret.Replace("%%", "%25%"); // Revisit to encode actual %'s
}
public static string PercentEncode(this string s)
{
var bytes = s.GetBytes();
var sb = new StringBuilder();
foreach (var b in bytes)
{
// [DC]: Support proper encoding of special characters (\n\r\t\b)
if((b > 7 && b < 11) || b == 13)
{
sb.Append(string.Format("%0{0:X}", b));
}
else
{
sb.Append(string.Format("%{0:X}", b));
}
}
return sb.ToString();
}
/// <summary>
/// URL encodes a string based on section 5.1 of the OAuth spec.
/// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
/// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
/// </summary>
/// <param name="value"></param>
/// <seealso cref="http://oauth.net/core/1.0#encoding_parameters" />
public static string UrlEncodeRelaxed(string value)
{
return Uri.EscapeDataString(value);
}
其中的中文内容最好进行两次UrlEncodeRelaxed否则的话如果本来发布的微博内容就是已经进行UTF8编码过的汉字的话 再编码会出现于本来中文一样的情况 原因在于UrlEncodeStrict进行编码的话编码前后不会发生变化的
3上传图片
关于上传图片 Upload接口上传图片Header里要有要加入Status信息 如这样
--REQUEST: http://api.t.sina.com.cn
POST /statuses/upload.json HTTP/1.1
Authorization: OAuth oauth_consumer_key="2404507248",oauth_token="435df4f2525c99f5b337bb79900a5d8c",oauth_nonce="5277664",oauth_timestamp="1299923945",oauth_signature_method="HMAC-SHA1",oauth_signature="UOsrFw6oX3%2FA4P%2Fb%2BVGbQK5ty7k%3D",oauth_version="1.0",
Content-Type: multipart/form-data; boundary=a00d9f7b-2a78-4cbb-a8a7-1cec8b92735b
--a00d9f7b-2a78-4cbb-a8a7-1cec8b92735b
Content-Disposition: form-data; name="status"
%E6%88%91
--a00d9f7b-2a78-4cbb-a8a7-1cec8b92735b
Content-Disposition: form-data; name="pic"; filename="bmp148.jpg"
Content-Type: application/octet-stream
[FILE DATA][System.Text.Latin1Encoding]
--a00d9f7b-2a78-4cbb-a8a7-1cec8b92735b--
另外图片文件不要使用Content-Disposition: file要使用Content-Disposition: form-data
觉得这点不太好,应该是开放平台的工程师有什么考虑才这么设计的吧