2016-04-22 98 views
6

用戶在我的android應用程序中授權。我正在將用戶令牌和其他信息發送到我的服務器。在這個服務器上,我想爲用戶實現一些邏輯。如何在服務器上訪問用戶的日曆信息?

我想正好有this flow

我按照步驟 quickstart.php in this link獲取用戶的日曆在服務器上。

,但我得到以下錯誤:

google oauth exception' with message 'could not json decode the token'

爲此,我試過this solution。 但我採取相同的錯誤。所以作爲第三個選項,我自己創建了json格式,如下面的this solution

$access_token = '{ 
    "access_token":'.$access_token.', 
    "token_type":"Bearer", 
    "expires_in":3600, 
    "id_token":'.$id_token.', 
    "refresh_token":" ", 
    "created":'. time() .' 
}'; 

如你所見我不知道如何交換刷新令牌。我搜索瞭如何獲得刷新令牌,並看到了this question。並執行this solution我的代碼,但沒有改變。

編輯4:我試圖在Android應用程序獲取訪問令牌according to this answer並將其發送到應用程序服務器。我走的代碼之前,我做的事:

GoogleSignInAccount acct = result.getSignInAccount(); 
code = acct.getServerAuthCode();//sending this code to AsyncTask to get access token 

我的函數來獲得訪問令牌:

private void getAccessToken()throws GoogleAuthException, IOException{ 
    new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 
      List<String> scopes = new LinkedList<String>(); 
       scopes.add("https://www.googleapis.com/auth/calendar"); 
       scopes.add("https://www.googleapis.com/auth/calendar.readonly"); 
       scopes.add("https://www.googleapis.com/auth/urlshortener"); 

       GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build(); 

       try{ 
        GoogleTokenResponse res = flow.newTokenRequest(code).execute(); 
        accessToken = res.getAccessToken(); 
       }catch(IOException e){ 
       } 

在PHP的服務器端,我改變user-example.php文件點點以下,因爲我有現在訪問令牌:

$client = new Google_Client(); 
$client->setClientId($client_id); 
$client->setClientSecret($client_secret); 
$client->setAccessType("offline"); 
$client->setRedirectUri($redirect_uri); 
$client->addScope("https://www.googleapis.com/auth/urlshortener"); 
$service = new Google_Service_Urlshortener($client); 
if (isset($_REQUEST['logout'])) { 
    unset($_SESSION['access_token']); 
} 
$client->setAccessToken('{"access_token":"'. $access_token .'","token_type":"Bearer","expires_in":3600,"created":'. time() .'}'); 
if ($client->getAccessToken() && isset($_GET['url'])) { 
$url = new Google_Service_Urlshortener_Url(); 
$url->longUrl = $_GET['url']; 
$short = $service->url->insert($url); 
$_SESSION['access_token'] = $client->getAccessToken(); 

但現在我發現了以下錯誤:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/urlshortener/v1/url : (403) Insufficient Permission' in C:\wamp\www\google-php\src\Google\Http\REST.php on line 110

+0

單引號中的變量不會被評估。打開變量的字符串。 '$ sp ='嗨,我是'。$ name'。我寫字符串';' –

+0

我仍然採取相同的json解碼錯誤。 :\ – melomg

+0

如果這兩個標記不是整數,則將它們放入雙引號中。 –

回答

2

我開始使用GoogleAuthorizationCodeFlow獲取訪問令牌後,我收到的權限不足錯誤,正如我在OP中提到的那樣。然後我試圖將日曆範圍添加到GoogleApiClient.Builder(this),但是我收到錯誤,因爲我添加Auth.GOOGLE_SIGN_IN_API因爲我已將Auth.GOOGLE_SIGN_IN_API添加到GoogleApiClient.Builder,因此無法添加範圍。所以這次我試圖將範圍添加到GoogleSignInOptions.Builder,它現在正在工作。我能夠獲得刷新令牌和訪問令牌。下面的代碼解決了我的問題:

GoogleSignInOptions gso = state.getGso(); 
if(gso == null){ 
    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
     .requestScopes(new Scope("https://www.googleapis.com/auth/calendar")) 
     .requestIdToken(getString(R.string.server_client_id)) 
     .requestEmail() 
     .requestServerAuthCode(getString(R.string.server_client_id), false) 
     .requestProfile() 
     .build(); 
} 
+0

嗨!你能幫我用谷歌註銷嗎?我認爲你必須在你的應用中實現它,如果你使用googleAuth ...我有這樣的問題,我在第一個活動中實現了logIn,但是我在另一個活動上實現了LogOut按鈕,然後如果我點擊這個按鈕,我調用'Auth。 GoogleSignInApi.revokeAccess(mGoogleApiClient)'並且出現錯誤'GoogleApiClient尚未連接。「據我所知,因爲它是不同的實體,當我登錄和註銷時...但我怎麼能保存相同實體?你在你的應用中實現了哪種方式? –

+0

我無法測試該項目,因爲我現在沒有服務器。就我所瞭解的代碼而言:D,我記得使用不同的標誌登錄活動,並且在onConnected函數的登錄活動中檢查此標誌。如果標誌爲真,我只需通過設置結果回調來調用'Auth.GoogleSignInApi.signOut'函數。但我很確定這不是實現這一目標的好方法。還有一些方法可以將值存儲爲全局。您可以查看[此鏈接](http://stackoverflow.com/a/1945297/3918109),並且在獲得'mGoogleApiClient'對象後,可以檢查它是否已連接。 – melomg

+0

據我記憶'onConnected()'方法是在以前的版本谷歌身份驗證提供...在上次實現它有一些區別...我沒有這種方法在我的實現(但我得到你的觀點 –

相關問題