stackoverflow를 검사하고 여러 가지 해결책을 시도했지만 oauth2 토큰에 대한 코드를 교환하기 위해 Google 토큰 서버에 대한 게시 요청을 할 때 계속 400을 얻습니다. 코드가 쿼리 문자열에서 올바르게 검색 중입니다. 나는 암호를 URL 인코딩, 리디렉션 URL을 시도하고 요청에 대한 인코딩을 ASCII로 변경했습니다. 나는 구글이 헤더에 까다롭기는하지만 json 응답은 의도적으로 애매하며 단지 "error : Bad Request"만 리턴한다는 것을 알고있다. 나는 요청에 무엇이 잘못된 것인지 디버그 할 수 없다. 나 또한 크롬 REST 클라이언트에서 게시 요청을 테스트하여 동일한 오류가 발생했습니다. 헤더 나 다른 형식 문제에 오류가 있다고 가정하지만 Google은 유용한 오류 코드를 반환하지 않습니다. 당신에는 urlencoding이 엉망이 될 수도 같은 코드Asp.net에서 Oauth2 토큰에 대한 Google Oauth2 코드
//trade code for token
public static String CodeTrade(String code)
{
String apiResponse;
string codeClient = "code=" + code + "&client_id=xxx.apps.googleusercontent.com&";
string secretUri = "client_secret=zzz&grant_type=authorization_code&redirect_uri=" + "https://web.locusvisual.com/gadgets/smallview/loginTrue.aspx";
string postData = codeClient + secretUri;
// Create a request using a URL that can receive a post.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
// Set the Method property of the request to POST.
request.Method = "POST";
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Create POST data and convert it to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
apiResponse = ((HttpWebResponse)response).StatusDescription.ToString();
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Console.ReadKey();
// Clean up the streams.
apiResponse = responseFromServer;
reader.Close();
dataStream.Close();
response.Close();
return apiResponse;
}
예! URL 인코딩이 문제였습니다. 답장을 보내 주셔서 감사합니다! – TauterTwiggy