2

I am very new to OAuth Arena and Google ApI but what I am trying to achieve here is very simple.

User Clicks on Google Connect button and my webservice should be able to get all the user Profile Info from Google server:

I have already written code to get AccessToken(I am yet to test it) but assuming that is working fine, now how should I ask Google API to give me user profile? I do see static function called Get Contacts in GoogleConsumer Class but I do not see any option to get profiledata. May be there is something that I am missing?

Here is my code using which i am getting accessToken:

IConsumerTokenManager tokenManager = 
                              new LocalTokenManager(consumerKey,consumerSecret);
var googleConsumer = 
               new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
var tokenResult = googleConsumer.ProcessUserAuthorization();
return tokenResult.AccessToken;

Now, how do I get user profile out of it?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Lost
  • 12,007
  • 32
  • 121
  • 193
  • Hi everyone,I am not sure why enyone hasn't touched this question. If I am lacking any information please let me know. Basically, it surprises me to see that all these WebConsumers(GoogleConsumer and TwitterConsumer) have all the sophisticated functions like getContacts and UpdateStatus and PostBlog but there is not basic function like GetProfile. All the examples that I see on the web actually get USerprofile gy calling a GET on Google's webservice URL but I do not see anything that demonstrates how to retrieve userProfile using GoogleWebConsume. Is is just not possible? – Lost Apr 17 '12 at 19:43
  • Doesn't [this SO post](http://stackoverflow.com/questions/7130648/get-user-info-via-google-api) answer your question? – shrisha Apr 18 '12 at 00:01
  • Well, this post says that we can use https://www.googleapis.com/auth/userinfo.profile as scope. Where does this scope exactly apply? Do I use it on RequestAuthorization Call? Because I tried to put this URL on but it does not take string arguments. – Lost Apr 18 '12 at 01:28

2 Answers2

3

Once you have your Access_Token (access type offline; and scope/permission is set so you can get user information), you can try the following (not tested, please let me know if any errors occur):

string userInfo = "";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action);
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(resp.GetResponseStream());
        userInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + "YOUR_ACCESSTOKEN";
        sr.Close();

            JObject jsonResp = JObject.Parse(userInfo);
            string info="";
            info += "<h3>" + jsonResp.Root["name"] + "</h3>";
            info += "<img src='" + jsonResp.Root["picture"] + "' width='120'/><br/>";
            info += "<br/>ID : " + jsonResp.Root["id"];
            info += "<br/>Email : " + jsonResp.Root["email"];
            info += "<br/>Verified_email : " + jsonResp.Root["verified_email"];
            info += "<br/>Given_name : " + jsonResp.Root["given_name"];
            info += "<br/>Family_name : " + jsonResp.Root["family_name"];
            info += "<br/>Link : " + jsonResp.Root["link"];
            info += "<br/>Gender : " + jsonResp.Root["gender"];

Response.Write(info);

Flow : Requesting the google userinfo url with the access token, get the response and displaying the information.

Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72
  • Well, I do do not have mu visual studio at the moment and I will try in an hour or so but just wanted to let you know that I am in a web service environment not in WEB Application environment. Would this work? – Lost Apr 19 '12 at 15:01
  • if the user is already logged-in (coz they should be coz you have the access token), then it should be fine. Just double check that you have a logged-in user and that all scope/permissions are set. Let me know. – Marc Uberstein Apr 19 '12 at 15:09
  • 1
    I think your code is very useful and I can use it while once I do have accessToken. I am actually working on the logic to get Request Token(little behind the schedule).As soon as I implement it, I will keep you updated. – Lost Apr 19 '12 at 16:50
  • Glad it can help you, let me know if you struggle. Cheers buddy – Marc Uberstein Apr 20 '12 at 05:18
  • 1
    @MarcUberstein what is the "action" which you are sending to HttpWebRequest – GowthamanSS Mar 18 '13 at 07:32
  • Yes, could you please specify what you mean by "action"? – Chris Jun 13 '14 at 09:16
  • "action" is the uri you pass to create the web request and get the access token. – Marc Uberstein Jun 13 '14 at 11:51
1

let me know what do you think about accessing profile's google info with their GET method, described here https://developers.google.com/+/api/latest/people/get? This is my C# example.

string urlGoogle = "https://www.googleapis.com/plus/v1/people/me";
HttpWebRequest client = HttpWebRequest.Create(urlGoogle) as HttpWebRequest;
client.Method = "GET";
client.Headers.Add("Authorization", "Bearer " + accessToken);
            
using (HttpWebResponse response = (HttpWebResponse)client.GetResponse())
{
     using (Stream dataStream = response.GetResponseStream())
     {
           using (StreamReader reader = new StreamReader(dataStream))
           {
                 if (response.StatusCode == HttpStatusCode.OK)
                 {
                     var json = new JavaScriptSerializer();
                     var data = json.Deserialize<IDictionary<string, object>>(reader.ReadToEnd());
    //....... here in data you have all json fields for the profile
nnnn
  • 63
  • 1
  • 6
  • Your code snippet does not appear to be formatted correctly. You may wish to remove some of the indentation. – ne1410s Dec 13 '14 at 14:51