GithubHelp home page GithubHelp logo

Comments (9)

georgehrke avatar georgehrke commented on May 28, 2024

I explained authentication in #36. If you don’t run your code as a Nextcloud app, you have to deal with authentication yourself in the xhrProvider. Just add a basic auth header.

Events you can add by calling “createVObject” on a calendar object. To get a complete API documentation, just run npm run build-doc

from cdav-library.

elspru avatar elspru commented on May 28, 2024

Hi georgehrke,

thank you for the prompt response,
I compiled and now can see the API.
I've also looked over the xhrProvider.

But I still don't see anywhere to put in my url, username, and password.
Would it be possible to make a basic "hello world" type application, that retrieves the calendar info from one of the demo instances of nextcloud?

A working example is worth a thousand words.

Thanks,
Logan

from cdav-library.

georgehrke avatar georgehrke commented on May 28, 2024
function xhrProvider() {
	var headers = {
		'X-Requested-With': 'XMLHttpRequest',
		'requesttoken': OC.requestToken
	}
	var xhr = new XMLHttpRequest()
	var oldOpen = xhr.open

	// override open() method to add headers
	xhr.open = function() {
		var result = oldOpen.apply(this, arguments)
		for (let name in headers) {
			xhr.setRequestHeader(name, headers[name])
			xhr.setRequestHeader('Authorization', 'Basic ' + btoa(unescape(encodeURIComponent(YOUR_USERNAME + ':' + YOUR_PASSWORD))))
		}
		return result
	}
	OC.registerXHRForErrorProcessing(xhr)
	return xhr
}

const client = new DavClient({
	rootUrl: OC.linkToRemote('dav')
}, xhrProvider)

await client.connect({enableCalDAV: true});
const calendars = await client.calendarHomes[0].findAllCalDAVCollections()
const events = await calendars[0].findAllVObjects()
console.log(events[0].data);

You can read more about Http auth here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#The_general_HTTP_authentication_framework

The URL of the server is given in rootUrl: OC.linkToRemote('dav'). You have to replace that with your URL. Just point it to /remote.php/dav/ of your Nextcloud instance.

from cdav-library.

georgehrke avatar georgehrke commented on May 28, 2024

@elspru Did that answer your question? :)

from cdav-library.

elspru avatar elspru commented on May 28, 2024

Hi @georgehrke it does seem to be better, but I'm still not really sure how to use it.
Like I'm trying to use this for a node application, but it seems that the code you gave me is for a web browser, and I'm not sure how to require() stuff in it. like should I have a sample web page also or something?

Maybe If I explain my use case it will make more sense.
I started on a Telegram chat bot and want it to send me calendar notifications, and have event specific behaviour. Like if I'm hosting a meditation session, it can read the title to know that, and then it will ask me at the appropriate times stuff like "what is the head count?" and "how much was donated?", so that the bot can function as my secretary and put that into the csv files and generate the website accordingly so the community knows where we are at, and if we have enough donations to cover the year more or less in real time.

Basically I need a minimal use case example in node.
or if it is in browser, then the example would ideally also tell me how to include it in the web page.

I'm sorry that I don't know very much about how this library works. I'm used to npm installing things, and having like a straight forward require('library'); pull_database(url, username, password); style API.

from cdav-library.

elspru avatar elspru commented on May 28, 2024

I tried this:

<script src=cdav-library.js></script>                                            
  <script>                                                                       
    function xhrProvider() {                                                     
        var headers = {                                                          
                'X-Requested-With': 'XMLHttpRequest',                            
                'requesttoken': OC.requestToken                                  
        }                                                                        
        var xhr = new XMLHttpRequest()                                           
        var oldOpen = xhr.open                                                   
                                                                                 
        // override open() method to add headers                                 
        xhr.open = function() {                                                  
                var result = oldOpen.apply(this, arguments)                      
                for (let name in headers) {                                      
                        xhr.setRequestHeader(name, headers[name])                
                        xhr.setRequestHeader('Authorization', 'Basic ' +         
                          btoa(unescape(encodeURIComponent('logan' + ':' + 'mysecurepssword'))))
                }                                                                
                return result                                                    
        }                                                                        
        OC.registerXHRForErrorProcessing(xhr)                                    
        return xhr                                                               
}                                                                                
                                                                                 
const client = new DavClient({                                                   
  rootUrl: 'https://nc.liberit.ca', // OC.linkToRemote('dav')                    
}, xhrProvider)                                                                  
                                                                                 
async function init(){                                                           
await client.connect({enableCalDAV: true});                                      
const calendars = await client.calendarHomes[0].findAllCalDAVCollections()       
const events = await calendars[0].findAllVObjects()                              
console.log(events[0].data);                                                     
console.log('hello world');                                                      
}                                                                                
  </script>                                                                      
<body onload="init()">                                                           
  hello                                                                          
</body>

It tells me DavClient is not defined.

from cdav-library.

Mordef avatar Mordef commented on May 28, 2024

Same here, no DavClient defined. There is 'cdav' but:
TypeError: cdav is not a constructor
I try to use with nextcloud.

from cdav-library.

georgehrke avatar georgehrke commented on May 28, 2024

I'm not sure if it will work when using <script src=cdav-library.js></script>, because we use ES6 Modules.

Please see https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ how to use them.
But i would recommend to use web pack in the meantime, until all major browsers adapted ES6 Modules.

An example how to use this library with web pack can be found in the contacts app.

from cdav-library.

georgehrke avatar georgehrke commented on May 28, 2024

Since there was no response or no further questions here for two months i will close this ticket.

from cdav-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.