2014-11-27 4 views
1

내 앱에 내 자체 백엔드 서버와 통신하는 Android 클라이언트가 있습니다. google + 계정을 사용하여 사용자 권한을 처리하기 위해 Google API를 사용하여 사용자가 내 앱에 별도의 계정을 등록 할 필요가 없습니다. 여기 https://developers.google.com/+/web/signin/server-side-flowandroid 앱과 서버 모두에 google + api 가입

인증 흐름에 대한 이해를 문서에 따라

은 아래와 같다. 그것이 내 목적을 위해

  1. 사용자 로그인 구글 API를 hijecking 같은 소리 때문에 나는,이 올바른지 확실하지 않다 안드로이드
  2. 안드로이드 클라이언트 한 번에 액세스 코드가 표시되고 서버로 전송합니다.
  3. 내 서버 교체 새로 고침 토큰 및 액세스 토큰. (교환이 성공하면 사용자는 인증 됨)
  4. 사용자가 인증되면 내 서버에서 액세스 토큰을 생성하여 Android 클라이언트에 제공합니다. 안드로이드 클라이언트는 토큰을 사용하여 내 서버의 api를 호출 할 수 있습니다.

특히 3 점에 대해서는 의문입니다. Google의 새로 고침 토큰과 액세스 토큰에 대한 액세스 코드를 교환하지만 사용하지 마십시오. 교환의 목적은 사용자가 Google에 의해 승인되었는지 여부를 확인하는 것입니다. 꽤 정확하지 않니? 그렇지 않다면, 그것을하는 좋은 방법은 무엇입니까?

답변

1

다른 사람들에게 도움이 될 수 있습니다.

access_token을 서버에 수신 한 후 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에서)

access_token은이 access_token이 경우 유효

{ 
"kind": "plus#person", 
"gender": "male", 
"emails": [ 
    { 
    "value": "[email protected]", 
    "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 

경우 무효

이다

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

고맙습니다.