0

我正在尝试使用刷新令牌进行离线访问。通过 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();
4

2 回答 2

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
于 2013-01-04T08:23:43.617 回答
0

您应该使用.NET 的客户端库,如果您可以粘贴完整的请求和响应,那可能会有所帮助。

于 2013-01-02T19:18:47.290 回答