2013-03-22 71 views
2

我用下面的代碼從代碼中獲得訪問令牌如下從谷歌的OAuth獲得訪問令牌從代碼

String code = HttpContext.Current.Request["code"]; 
        string redirecturl = HttpContext.Current.Request["url"];         

        string Url = "https://accounts.google.com/o/oauth2/token"; 
        string grant_type = "authorization_code"; 
        string redirect_uri_encode = UrlEncodeForGoogle(url); 
        string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type={5}"; 

        HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest; 
        string result = null; 
        request.Method = "POST"; 
        request.KeepAlive = true; 
        request.ContentType = "application/x-www-form-urlencoded"; 
        string param = string.Format(data, code,configurationInfo.oauthclientid , configurationInfo.oauthclientsecretid, redirect_uri_encode, grant_type, "offline"); 

        var bs = Encoding.UTF8.GetBytes(param); 
        using (Stream reqStream = request.GetRequestStream()) 
        { 
         reqStream.Write(bs, 0, bs.Length); 
        } 

        using (WebResponse response = request.GetResponse()) 
        { 
         var sr = new StreamReader(response.GetResponseStream()); 
         result = sr.ReadToEnd(); 
         sr.Close(); 

        } 

我得到響應,

The remote server returned an error: (400) Bad Request. 

我不知道我哪裏錯

等待着您的寶貴意見

+0

您是否已經解決了問題? – cherhan 2013-03-25 01:09:43

+0

@cherhan ya如果我們正確提供所有註冊信息,否則它可以正常工作 – GowthamanSS 2013-03-26 05:14:09

+0

yup我也有類似於您的代碼,很高興聽到它的作用。 – cherhan 2013-03-26 06:00:21

回答

相關問題