我不明白这个功能的错误是为了得到我的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);
}
}
}
答案 0 :(得分:0)
我强烈建议任何进行.NET开发并希望访问YouTube API的人使用official client library。
如果您不想将客户端库用于所有操作,至少,我建议您将它用于handle OAuth 2,然后使用它在{{1}中生成的令牌你自己滚动的请求的标题。