0

I am trying to get a response from google in c# to get a token.

I am using the url - https://accounts.google.com/o/oauth2/token.

My code is the following;

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;
    }

The line response = (HttpWebResponse)request.GetResponse();

is giving me the 405 Error Method Not Allowed.

I have tried for the past two days to fix this but nothing, can anyone see where I have went wrong?

Thanks

opalenzuela
  • 3,139
  • 21
  • 41

0 Answers0