Google plus ( Get picture url )

2019-06-14 02:20发布

问题:

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!

回答1:

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.



回答2:

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!



回答3:

$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]);