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

mel*_*omg 6 php authentication android google-calendar-api google-oauth

用户在我的Android应用程序中授权.我正在将用户的令牌和其他信息发送到我的服务器.在这个服务器上,我想为用户实现一些逻辑.

我希望得到这个流程.

我按照此链接中的步骤quickstart.php来获取用户在服务器上的日历.

但我得到以下错误:

google oauth exception'with message'无法json解码令牌'

因此我尝试了这个解决方案.但我也犯同样的错误.因此,作为第三个选项我创建的JSON格式自己像这个解决方案如下.

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

如你所见,我不知道如何交换刷新令牌.我搜索了如何获得刷新令牌并看到了这个问题.并将此解决方案应用于我的代码但没有任何改变.

编辑4:我试图在Android应用程序中根据此答案获取访问令牌并将其发送到应用服务器.我像以前一样接受代码:

GoogleSignInAccount acct = result.getSignInAccount();
code = acct.getServerAuthCode();//sending this code to AsyncTask to get access token
Run Code Online (Sandbox Code Playgroud)

我获取访问令牌的功能:

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){
                }
Run Code Online (Sandbox Code Playgroud)

我在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();
Run Code Online (Sandbox Code Playgroud)

但现在我得到以下错误:

致命错误:在C:\ wamp\www\google-php\src \中显示错误"Google_Service_Exception"消息"错误调用POST https://www.googleapis.com/urlshortener/v1/url:(403)权限不足"第110行的Google\Http\REST.php

mel*_*omg 2

正如我在 OP 中提到的,在开始使用 GoogleAuthorizationCodeFlow 获取访问令牌后,我收到了“权限不足”错误。然后我尝试添加日历范围,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();
}
Run Code Online (Sandbox Code Playgroud)