Try access gallery API from other project


#1

Hi, I am trying access api from other web application (via javascript) but I have permission problems...

How I can make ajax request to gallery app (to get list files for example). What I need send to request (or header) to api automatically make login and return files as json to me? The Document API not is clean to me.

Thank you!


#2

Which error message have you got?

Did you use the ownCloud manual?
https://doc.owncloud.org/server/9.1/admin_manual/configuration_user/user_provisioning_api.html


#3

Yes, I have this error:
<?xml version="1.0"?>


failure
997
Unauthorised


My request:
var xhr = new XMLHttpRequest();

xhr.open('GET', 'http://admin:pass@myip/ocs/v1.php/cloud/apps/files');
xhr.onload = function() {
    if (xhr.status !== 200) {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();

#4

Do you use ownCloud with SSL?

That can be the solution: http://stackoverflow.com/questions/33332649/owncloud-8-1-user-provisioning-api-not-authorized


#5

changed to ssl but still problems:

var xhr = new XMLHttpRequest();

xhr.open('GET', 'https://admin:pass@domain.com/index.php/apps/gallery/api/files/list');
xhr.onload = function() {
    if (xhr.status !== 200) {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();

{"message":"CORS requires basic auth"}

#6

You need to send a basic auth header with your username and password.


#7

Withou ssl? can you give example?
username: admin
password: test


#8

Please use google / your favorite search engine for such questions. This should show you how to use basic authentication within such requests.


#9

The first error message was a configuration issue because of SSL.
The second message will be explained here: https://github.com/fcturner/passwords/wiki/ownCloud-Passwords-%7C-RESTful-API


#10

Final solution:

Ext.Ajax.request({
	url: 'https://domain/index.php/apps/gallery/api/files/list',
	method: 'GET',
	headers: {
		Authorization: Ext.String.format('Basic {0}', btoa('admin:password'))
	},
	params: {
		location: 'Photos',
		mediatypes: 'image/png;image/jpeg;image/gif;image/x-xbitmap;image/bmp'
	},
	success: function() {
		console.log(arguments)
	},
	failure: function() {
		console.log(arguments)
	}
});

Thank you so much