如何获取服务器上用户的日历信息?

How can I reach users' calendar information on server?

用户在我的 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. And implemented 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

正如我在 OP 中提到的那样,在我开始使用 GoogleAuthorizationCodeFlow 获取访问令牌后,出现权限不足错误。然后我尝试将 Calendar 范围添加到 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();
}