谷歌+ API登入为Android应用程序和服务器 [英] google+ api signin for both android app and server

查看:142
本文介绍了谷歌+ API登入为Android应用程序和服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个Android客户端用我自己的后端服务器进行通信。
我想使用谷歌API来处理我的(使用谷歌+帐户)的用户授权,使用户不需要注册一个单独的帐户我的应用程序。

my app has an android client communicating with my own back-end server. I want to use google api to handle the user authorisation for me (using google+ account), so that user do not need to sign up a separate account for my app.

按照这里的文档 https://developers.google.com / + / WEB /登入/服务器端的流量

我的认证流程的理解如下所示。我不知道这是正确的,因为它听起来像hijecking谷歌的API,我的目的

My understanding of the authentication flow is shown below. I am not sure it is correct, because it sounds like hijecking the google api for my purpose


  1. 用户在Android
  2. 登录
  3. Android客户端得到一个时间内获得code,并将其发送到我的服务器。

  4. 我的服务器交换刷新令牌和访问令牌。 (如果交换成功,用户将通过验证)

  5. 如果用户通过验证,我在我的服务器上生成一个访问令牌,它给Android客户端。 Android客户端可随后使用该令牌来调用我的服务器的API。

特别是,我怀疑的地步3.我访问Exchange code为谷歌刷新令牌和访问令牌,但从来没有使用它们。交流的目的仅仅是检查用户是否已授权谷歌。不听起来很正确吗?如果没有,是什么做的好方法吗?

Especially, I am doubtful about the point 3. I exchange access code for google refresh token and access token, but never use them. The purpose of the exchange is just to check whether the user has been authorised by google. Does not sounds quite correct? If not , what is the good way to do it?

推荐答案

这可以帮助别人。

您收到后的的access_token 在你的服务器上,执行卷曲/网络请求到谷歌服务器。

After you receive access_token in your server, execute cURL/network request to google server.

例如(在PHP):

<?php 

$access_token=$_GET['access_token']; //get the access token

$google_api_url="https://www.googleapis.com/plus/v1/people/me?access_token=". $access_token; // create google api url with access_token

$c=curl_init($google_api_url); // create network request to google server

curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

$result=curl_exec($c); //execute request and get the response from google server

  var_dump($result); //print the result

结果输出(以JSON):

RESULT OUTPUT (in JSON) :

如果是的access_token的有效

if access_token is valid

{
 "kind": "plus#person",
 "gender": "male",
 "emails": [
  {
   "value": "arr.mohd@gmail.com",
   "type": "account"
  }
 ],
 "objectType": "person",
 "id": "101571740244190011262",
 "displayName": "Rafique Mohammed",
 "name": {
  "familyName": "Mohammed",
  "givenName": "Rafique"
 },
 "url": "https://plus.google.com/101571740244190011262",
 "image": {
  "url": "https://lh6.googleusercontent.com/-pxiRX5gNkWE/AAAAAAAAAAI/AAAAAAAAAEg/99WWMsH16P8/photo.jpg?sz=50",
  "isDefault": false
 },.... //etc

如果是的access_token的无效

if access_token is invalid

   {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "authError",
        "message": "Invalid Credentials",
        "locationType": "header",
        "location": "Authorization"
       }
      ],
      "code": 401,
      "message": "Invalid Credentials"
     }
    }

感谢您

这篇关于谷歌+ API登入为Android应用程序和服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆