我不明白此功能获得我的authkey有什么问题?我只会收到称为" Invalid_request"的错误,有人有任何想法吗?似乎它不了解我的要求吗?
//innk
public partial class OAuth : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string _clientID = HttpUtility.UrlEncode("***********");
string _clientSecret = HttpUtility.UrlEncode("******");
string _redirectUri = "urn:ietf:wg:oauth:2.0:oob";
string _code = HttpUtility.UrlEncode("token");
string url = "code=" + _code + "&client_id=" + _clientID + "&client_secret=" + _clientSecret + "&redirect_uri=" + _redirectUri + "&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer";
TcpClient client = new TcpClient("accounts.google.com", 443);
Stream netStream = client.GetStream();
SslStream sslStream = new SslStream(netStream);
sslStream.AuthenticateAsClient("accounts.google.com");
{
byte[] contentAsBytes = Encoding.ASCII.GetBytes(url.ToString());
StringBuilder msg = new StringBuilder();
msg.AppendLine("POST /o/oauth2/token HTTP/1.1");
msg.AppendLine("Host: accounts.google.com");
msg.AppendLine("Content-Type: application/x-www-form-urlencoded");
msg.AppendLine("Content-Length: " + contentAsBytes.Length.ToString());
msg.AppendLine("");
Debug.WriteLine("Request");
Debug.WriteLine(msg.ToString());
Debug.WriteLine(url.ToString());
byte[] headerAsBytes = Encoding.ASCII.GetBytes(msg.ToString());
sslStream.Write(headerAsBytes);
sslStream.Write(contentAsBytes);
}
Debug.WriteLine("Response");
StreamReader reader = new StreamReader(sslStream);
while (true)
{ // Print the response line by line to the debug stream for inspection.
string line = reader.ReadLine();
if (line == null)
break;
Debug.WriteLine(line);
}
}
}
我强烈建议您使用.NET开发并希望访问YouTube API的任何人使用官方客户端库。
如果您不希望将客户端库用于所有操作,至少,我建议您使用它为您处理OAuth 2,然后使用它在请求的Authorization
标题中生成的令牌你滚动自己。