【问题标题】:Google plus ( Get picture url )谷歌加(获取图片网址)
【发布时间】:2014-01-27 11:44:14
【问题描述】:

我在获取图片 URL 作为 Google Plus 的响应时遇到问题。

下面提到的正是我需要的..

{
 "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"
}

直到我使用下面提到的为了得到相同。请看看..

在 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();
            }

上述输出::

{ "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"}

请告诉我我错过了什么。 任何建议都可能对我有所帮助。

谢谢!

【问题讨论】:

  • 您能否展示您最终是如何使用 Swayam 的答案从 Google 获得图像的?这真的会帮助像我这样正在寻找答案的其他人。谢谢
  • @OjonugwaOchalifu .. 请检查我的答案。谢谢。

标签: android google-plus


【解决方案1】:

如果知道用户ID,可以直接查询获取头像,可以从代码中获取。

打个电话就行了

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

其中 getGooglePlusId() 是一个用户定义的函数,它返回用户的 ID 和 sz 指定返回图像的大小。

编辑:

目前已过时(2015 年 12 月) 你应该使用 https://www.googleapis.com/plus/v1/people/{GOOGLE_USER_ID}?key={YOUR_API_KEY} 其中:

GOOGLE_USER_ID - Google Plus 中的用户 ID

YOUR_API_KEY - Android API 密钥(您可以阅读如何获取它here

此请求的响应将返回 JSON,其中包含一个“图像”字段。这实际上是用户个人资料图像。你可以试试这个here

顺便说一下,这个 API 每天有 10000 次访问限制。在您的 google api 控制台中,您可以向 Google 索取更多配额,但我不知道它是如何工作的。

【讨论】:

  • @谢谢..你的回答是正确的。但是需要问你一些事情..(1) 现在我尝试使用另一个为我提供具有相同代码图片的帐户。你能告诉我为什么会这样吗?
  • 我认为在这两种情况下,您可能会在我的代码中传递相同的用户 ID。
  • 您需要了解您不需要任何登录或访问令牌即可通过此 API 获取图像。如果您知道他们的个人资料 ID,则可以获取任何人的图像。
  • 是的..我明白这一点。但是相同的代码返回图像 URL 没有任何问题。我真的不明白为什么会发生这种行为。如果在 GOOGLE PLUS 中没有定义图像时会发生这种情况,请您指导我...谢谢 意味着如果没有定义图像,将返回什么作为响应?
  • 我认为它会返回默认的蓝色用户图像。不过我还没有测试过。你可以自己检查一下。 :)
【解决方案2】:

您需要在布局中定义 ImageView。

说吧..user_picture

现在您可以像 ..@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();
            }

        }

希望它对某人有所帮助..干杯!

【讨论】:

  • 是这样想的。你可以考虑改正一下。那么这个方法是在哪里定义的呢?
  • 我的意思是这个获取头像的方法。你在哪里定义的?在onCreate() 方法中?
【解决方案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]);

【讨论】:

  • 虽然您的回答可能会解决问题,但最好能说明问题所在以及您的回答如何解决问题。这是进一步改进这个和未来答案的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
相关资源
最近更新 更多