0

我正在尝试在 c# 中从谷歌获得响应以获取令牌。

我正在使用网址 - https://accounts.google.com/o/oauth2/token

我的代码如下;

public WebResponse GetResponse(string url, params GoogleParameter[] parameters)
    {
        //Format the parameters
        string formattedParameters = string.Empty;

        foreach (var par in parameters)
            formattedParameters += string.Format("{0}={1}&", par.Name, par.Value);

        formattedParameters = formattedParameters.TrimEnd('&');

        //Create a request with or without parameters
        HttpWebRequest request = null;
        if (formattedParameters.Length > 0)
        {
            //string debug = (string.Format("{0}?{1}", url, formattedParameters));
            request = (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}", url, formattedParameters));
        }
        else
            request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "GET";

        //Add the authentication header.
        request.Headers.Add("Authorization", "GoogleLogin auth=" + auth);
        HttpWebResponse response = null;
        //Get the response, validate and return
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

        if (response == null)
            throw new Exception("No Response from Google");
        else if (response.StatusCode != HttpStatusCode.OK)
            throw new Exception("Incorrect Response: " + response.StatusCode + " " + response.StatusDescription); 

        return response;
    }

行 response = (HttpWebResponse)request.GetResponse();

给了我不允许的 405 错误方法。

这两天我一直在尝试解决这个问题,但没有任何效果,谁能看到我哪里出错了?

谢谢

4

0 回答 0