0

I am stuck with a problem for getting picture URL as a response from Google Plus.

Undermentioned is exactly I need ..

{
 "id": "ID",
 "name": "NAME",
 "given_name": "GiVEN NAME",
 "family_name": "FAMILY_NAME",
 "link": "https://plus.google.com/ID",
 "picture": "https://PHOTO.jpg",
 "gender": "GENDER",
 "locale": "LOCALE"
}

Till time I am using undermentioned in order to get same. please have a look ..

Using undermentioned in onConnected();

try {
                 URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo");
                  //get Access Token with Scopes.PLUS_PROFILE
                  String sAccessToken;
                sAccessToken = GoogleAuthUtil.getToken(MainActivity.this,
                                          mPlusClient.getAccountName() + "",
                                          "oauth2:" + Scopes.PLUS_PROFILE
                                          + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");

                   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                      urlConnection.setRequestProperty("Authorization", "Bearer "
                              + sAccessToken);
                      BufferedReader r = new BufferedReader(new InputStreamReader(
                              urlConnection.getInputStream(), "UTF-8"));
                      StringBuilder total = new StringBuilder();
                      String line;
                      while ((line = r.readLine()) != null) {
                          total.append(line);
                      }
                      line = total.toString();
                      System.out.println("::::::::::::::::::::::::" + line);
            } catch (UserRecoverableAuthException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Intent recover = e.getIntent();
                startActivityForResult(recover, REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (GoogleAuthException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Output of above mentioned ::

{ "id": "106193796000956371669", "email": "vivek@xyz.com", "verified_email": true, "name": "Vivek Singh", "given_name": "Vivek", "family_name": "Singh", "link": "https://plus.google.com/10619379600095669", "gender": "male", "locale": "en", "hd": "xyz.com"}

Please let know about what I am missing. Any suggestion might be helpful for me.

Thanks!

AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
  • Can you show how you finally got the image from Google plus using Swayam's answer? This would really help others like myself who are looking for answers.Thanks – Ojonugwa Jude Ochalifu Feb 11 '14 at 02:34

3 Answers3

4

You can make a query to get the profile picture directly if you know the ID of the user, which you can get from your code.

Just make a call to

https://plus.google.com/s2/photos/profile/" + user.getGooglePlusId() + "?sz=100

where getGooglePlusId() is a user-defined function which returns the ID of the user and sz specifies the size of the image returned.

EDIT:

This is outdated for now (december 2015) You should use https://www.googleapis.com/plus/v1/people/{GOOGLE_USER_ID}?key={YOUR_API_KEY} where:

GOOGLE_USER_ID - ID of the user in Google Plus

YOUR_API_KEY - Android API key (You can read how to get it here)

The response for this request would return JSON, which contains an "image" field. This is actually user profile image. You can try to experiment with this here.

By the way, there is a limit of 10000 accesses per day to this API. In your google api console you can ask Google for more quota, but I don't know how it works exactly.

Le_Enot
  • 799
  • 6
  • 16
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • @ Thanks .. Your answer is correct. But need to ask you some thing ..(1) right now I tried using another account which is providing me picture with same code. Can U please let me know why this is happening? – AndroidHacker Jan 27 '14 at 11:58
  • 1
    I think in both the occasions you might be passing the same user id in my code. – Swayam Jan 27 '14 at 11:59
  • You need to understand that you don't need any login or access token to get the image via this API. You can get the image of anyone if you know their profile id. – Swayam Jan 27 '14 at 12:00
  • Yeah .. I understand that. But same code is returning image URL with out any problem. I really can't understand why this behavior is occurring. ALSO CAN YOU PLEASE GUIDE ME THAT IF THAT CAN HAPPEN WHEN THERE IS NO IMAGE DEFINED IN GOOGLE PLUS... THANKS Means what will be returned as response if no image is defined? – AndroidHacker Jan 27 '14 at 12:04
  • I think it will return the default blue coloured user image. I haven't tested it though. You could check it out yourself. :) – Swayam Jan 27 '14 at 12:08
  • No problem mate. Good luck ahead! :) – Swayam Jan 27 '14 at 12:39
  • This is outdated for now. You should use `https://www.googleapis.com/plus/v1/people/{GOOGLE_USER_ID}?key={YOUR_API_KEY}` – Le_Enot Dec 10 '15 at 11:13
  • @Le_Enot : Thanks for the edit! Hope it helps others as well. :) – Swayam Dec 10 '15 at 11:44
-1

You need to define ImageView in your layout.

say it .. user_picture

Now you can set that image directly to it like .. @For Facebook / Google Plus

if(usertype.equalsIgnoreCase("facebook")){

            try{

                URL img_value = null;
                img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
                Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                user_picture.setImageBitmap(mIcon1);

            }catch(Exception e){
                e.printStackTrace();
            }

        }else{

            try{

                URL img_value = null;
                img_value = new URL("https://plus.google.com/s2/photos/profile/"+ google_Plus_User_Id +"?sz=50");
                Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                user_picture.setImageBitmap(mIcon1);

            }catch(Exception e){
                e.printStackTrace();
            }

        }

Hope it helps some one ..Cheers!

AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
-1
$forJson = file_get_contents('http://picasaweb.google.com/data/entry/api/user/'.$userInfo['id'].'?alt=json', true);
$withowtBacs = str_replace('$','',$forJson);
$toArr = (array)json_decode($withowtBacs);
$andNext = (array)$toArr[entry];
$imgPath = (array)$andNext[gphotothumbnail];
$this->session->set_userdata('imgPath', $imgPath[t]);
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
Alex
  • 1
  • 1
    While your answer may solve the question, it is always better if you can provide a description of what the issue was and how your answer solves it. This is a suggestion for further improving this and future answers. – Luís Cruz Oct 07 '14 at 12:44