How to Post from Server to Server in ASP.NET
I'm currently trying to import customer's email contact from Gmail. I use the OAuth 2.0 to authenticate between Google and the user according to the doc 开发者_C百科http://code.google.com/apis/accounts/docs/OAuth2.html
First i send a GET request to the google using
https://accounts.google.com/o/oauth2/auth?scope=https://www.google.com/m8/feeds&
client_id=yyyyyyyy.apps.googleusercontent.com&
redirect_uri=http%3A%2F%2Flocalhost%3A11195%2Fhome%2Fgreturn&response_type=code
then google response with this
http://localhost/home/greturn?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6
Here is the code in ASP.NET MVC
In the Views/Index.cshtml
javascript:popup('https://accounts.google.com/o/oauth2/auth?scope=https://www.google.com/m8/feeds&client_id=yyy.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A11195%2Fhome%2Fgreturn&response_type=code');
and Google redirect back to home/greturn with a parameter 'code'. (ex: code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6) This is greturn() in homecontroller
public ActionResult GReturn()
{
string token = Request.QueryString["code"];
OAuth auth = new OAuth(token);
IDictionary<string, string> mycontact = oauth.GetGoogleMail("https//accounts.google.com/o/oauth2/token");
return View(mycontact);
}
i keep the 'code' in token variable
from this point what i have to do is to POST the data back again to the google url
https://accounts.google.com/o/oauth2/token
with the data given
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&
client_id=yyyyyy.apps.googleusercontent.com&
client_secret=zzzzz&
redirect_uri=http://localhost/home/greturn&
grant_type=authorization_code
and in return waiting for google to send back the JSON object "access_token":"1/fFAGRNJru1FTz70BzhT3Zg", "expires_in":3920, "refresh_token":"1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ"
The question is how can i POST the require data to the given URI and receive the return value
This is the code i tried to implement and not yet finished, so please take a look
public class OAuth
{
private string Token;
public OAuth() { }
public OAuth(string Token)
{
this.Token = Token;
}
public IDictionary<string, string> GetGoogleMail(string LoginUrl)
{
HttpValueCollection loginFormValues = new HttpValueCollection();
loginFormValues["code"] = this.Token;
loginFormValues["client_id"] = "yyyy.apps.googleusercontent.com";
loginFormValues["client_secret"] = "zzzz";
loginFormValues["redirect_uri"] = "http%3A%2F%2Flocalhost%3A11195%2Fhome%2Fgreturn";
loginFormValues["grant_type"] = "authorization_code";
byte[] loginPostData = Encoding.UTF8.GetBytes(loginFormValues.ToString(true));
Uri url = new Uri(LoginUrl);
HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(url);
loginRequest.Method = "POST";
loginRequest.ContentType = "application/x-www-form-urlencoded";
loginRequest.ContentLength = loginPostData.Length;
// Add post data to request
Stream stream;
using (stream = loginRequest.GetRequestStream())
{
stream.Write(loginPostData, 0, loginPostData.Length);
}
Dim client As New WebClient()
Dim values As New NameValueCollection()
values.Add("code", Request.QueryString("code"))
values.Add("client_id", "client-id")
values.Add("client_secret", "secret-key")
values.Add("redirect_uri", "url/")
values.Add("grant_type", "authorization_code")
Dim responseS As Byte() = client.UploadValues("https://accounts.google.com/o/oauth2/token", values)
Response.Write(Encoding.UTF8.GetString(responseS))
精彩评论