Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reach users' calendar information on server?

Users authorize in my android application. And I am sending users' token and other information to my server. At this server I want to implement some logic for users.

I want to have exactly this flow.

I followed the steps quickstart.php in this link to get users' calendars on server.

But I get following error :

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

For this reason I tried this solution. But i take same error. So as 3rd option I created the json format myself like in this solution as below.

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

as you see I do not know how to exchange refresh token . I searched how to get refresh token and saw this question. And implemented this solution to my code but nothing changed.

Edit 4 : I tried to get access token according to this answer at android application and send it to app server. I'm taking the code as before I did :

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

my function 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){
                }

at the php server side I changed user-example.php file little bit as below because I have the access token now:

$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();

But now I'm getting below error:

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

like image 723
melomg Avatar asked Apr 22 '16 08:04

melomg


People also ask

How do I access another user's calendar?

In Calendar, on the Home tab, in the Manage Calendars group, click Open Calendar, and then click Open Shared Calendar. Type a name in the Name box, or click Name to select a name from the Address Book. The shared Calendar appears next to any calendar that is already in the view.

How do I request access to someone's calendar in Outlook?

Right-click the folder Calendar, and then click Properties on the shortcut menu. Click the Permissions tab. Click Add. In the Type name or select from list box, type or select the name of the person you want to grant sharing permissions to.

Can others see my Outlook calendar details?

Your calendars can be viewed only by others to whom you have granted permissions. If the other person whose calendar you want to open has not granted you permission to view it, Outlook prompts you to ask the person for the permission that you need.

How to share the calendar with other users?

By default, the calendar is shared with the users within the organization. Other users can add the calendar in Outlook Web App (OWA) or Outlook client. However, by default permission can only let other users see busy & free status on the calendar. If you want to see the details or edit it, you need to have the correct permission.

How do I make the calendar entries visible to the user?

To make the calendar entries visible to the user, the mailbox owner or the Office 365 Administrator should change the default permission level to either ‘Free/Busy time’ or ‘Free/Busy time, subject, location.’ After that, the user can see the free/busy status in the Scheduling Assistant and also the data in the calendar.

Why can’t the user see the information in the calendar?

If the default permission is set to either ‘None’ or ‘Controller,’ then the user cannot see the information. Additionally, the user cannot even see the mailbox calendar. It is due to the nature of this permission because ‘None’ and ‘Controller’ both do not provide any visibility to the user. Here are two steps which you can follow:

What to do when a shared calendar is not working?

“ A connection couldn’t be made with the shared calendar. Remove the calendar and try to add it again or ask the owner to share it again. “ Furthermore, the users cannot access the information in the Scheduling Assistant for the user’s mailbox, and the user gets the error ‘Could not be updated.’


1 Answers

I was getting Insufficient Permission error after I started to use GoogleAuthorizationCodeFlow to get access token as I mentioned in my OP. And then I tried to add Calendar scope to GoogleApiClient.Builder(this) but I get error like I can't add scope if I add Auth.GOOGLE_SIGN_IN_API because I had added Auth.GOOGLE_SIGN_IN_API to GoogleApiClient.Builder. So this time I tried to add the scope to GoogleSignInOptions.Builder and it is working now. I'm able to get both refresh token and access token. Below code solved my problem:

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();
}
like image 74
melomg Avatar answered Oct 16 '22 12:10

melomg