2016-04-22 100 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()'方法是在以前的版本谷歌身份验证提供...在上次实现它有一些区别...我没有这种方法在我的实现(但我得到你的观点 –

相关问题