我正在尝试使用refreshtoken进行离线访问。使用httprequest,我必须使用保存的refreshToken获取新的accessToken。
但是我一直收到错误的请求400而没有进一步解释什么是错的。有人能告诉我这段代码有什么问题或发布一个有效的代码吗?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
authenticator.ApplyAuthenticationToRequest(request);
request.Method = "POST";
String para = "code='"+refreshtoken+"'&client_id='" + ClientCredentials.CLIENT_ID + "'&client_secret='" + ClientCredentials.CLIENT_SECRET + "'&grant_type='refresh_token'";
System.IO.Stream requestStream = request.GetRequestStream();
System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(requestStream);
streamwriter.Write(para);
streamwriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
String result = reader.ReadToEnd();
答案 0 :(得分:0)
您应该使用客户端library for .NET,如果您可以粘贴完整的请求和响应,那可能会有所帮助。
答案 1 :(得分:0)
Public Function httpReq(uri As String, para As String, host As String, method As String) As String
Dim responseData As String = ""
Dim postData As String = para
Dim encoding As New ASCIIEncoding()
Dim byte1() As Byte = encoding.GetBytes(postData)
Dim httpWebRequest As System.Net.HttpWebRequest = Net.WebRequest.Create(uri)
httpWebRequest.Method = method
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
httpWebRequest.ContentLength = byte1.Length
Dim newStream As IO.Stream = httpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
newStream.Close()
Dim httpWebResponse As System.Net.HttpWebResponse = httpWebRequest.GetResponse()
If httpWebResponse.StatusCode = System.Net.HttpStatusCode.OK Then
Dim responseReader As System.IO.StreamReader = New System.IO.StreamReader(httpWebResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
End If
httpWebResponse.Close()
Return responseData
End Function