Try the fastest and simplest way to install ONLYOFFICE

API issues - authorization header

Integration questions/issues

API issues - authorization header

Postby brix » Thu Oct 30, 2014 11:39 am

Hello!
I try to access api but no luck. I managed to get a authentication token (the below is generated example).

{"count":1,"status":0,"statusCode":201,"response":{"token":"jgit4u8zu5843u0ur90309gu3r8zg93z7804893if'2eif'012j0gf2hdivjwkldgj209u48uh9GT3u83u9g3\/4758973489578394759834\/5724584250","expires":"2015-10-30T12:29:05.7524000+01:00","sms":false,"phoneNoise":null}}

Then I try to POST to the api address with the authentication headers. I tried with firefox restclient, chrome advanced restclient, wiztools restclient and with curl.

curl -H "Authorization:jgit4u8zu5843u0ur90309gu3r8zg93z7804893if'2eif'012j0gf2hdivjwkldgj209u48uh9GT3u83u9g3\/4758973489578394759834\5724584250" \
-X POST \
-H "Content-type: application/json" \
-H "Accept:application/json" \
-d 'companyName=Test' \
'http://portal.domain.com/api/2.0/crm/contact/company/quick.json'

Non of the clients are working, so obviously I'm doing something wrong. I always get HTTP/1.1 401 Unauthorized back..

I did look at the api documentation, but could not get it to authenticate.

Can you maybe give me some examples with curl or php?

Can you help me with this problem?
Thank you very much for your answers.
brix
brix
 
Posts: 16
Joined: Mon Oct 06, 2014 10:42 pm

Re: API issues - authorization header

Postby AndreyS » Fri Nov 07, 2014 9:41 am

javascript example:

//using http://api.jquery.com/jQuery.ajax/

//your portal address
var baseUrl = "http://myportal.onlyoffice.com";

var getSelf = function () {
return $.ajax({
type: "GET",
url: baseUrl + "/api/2.0/people/@self.json",
dataType: "json",
beforeSend: setHeader
});
};

function setHeader(xhr) {
//your auth token
var token = "mytoken=QWERTYUIOPASDFGHJKLZXCVBNM";
xhr.setRequestHeader("Authorization", token);
}

//string value
var selfResponse = getSelf();

//Json value (using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
var self = JSON.parse(selfResponse.responseText);
AndreyS
 
Posts: 2
Joined: Mon Aug 25, 2014 9:55 am

Re: API issues - authorization header

Postby AndreyS » Fri Nov 07, 2014 9:52 am

C# example:

[TestMethod]
public void Test()
{
var baseUrl = "http://myportal.onlyoffice.com";
var token = "mytoken=QWERTYUIOPASDFGHJKLZXCVBNM";
var result = string.Empty;
using (var client = new WebClient())
{
client.Headers.Add("Authorization", token);
result = client.DownloadString(baseUrl + "/api/2.0/people/@self.json");
Assert.IsNotNull(result);
}
}
AndreyS
 
Posts: 2
Joined: Mon Aug 25, 2014 9:55 am

Re: API issues - authorization header

Postby kengrant » Sat May 09, 2015 11:55 am

Hi

I am in a similar situation. I have managed to receive back a token and can run the TestMethod example in this post. However, I am now trying to POST back a task creation and I am struggling:

API : POST api/2.0/project/{projectid}/task

Code snip in VB - Just for testing....:

Dim request As HttpWebRequest
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing

data = New StringBuilder()

address = New Uri("http://domainname,com/api/2.0/project/2/task") '- Project 2 exists
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
' Set type to POST
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Host="domainname.com"
request.Headers.Add("Authorization", token) ' token has been obtained
data = New StringBuilder()


data.Append("description=" + HttpUtility.UrlEncode(""Test Description))
data.Append("&deadline=" + "2015-05-27") 'Note - Roundtrip back - Docs say: As for the request, only date can be send in it: 2008-04-10
data.Append("&priority=" + HttpUtility.UrlEncode("Low"))
data.Append("&title=" + HttpUtility.UrlEncode("Test"))
data.Append("&milestoneid=" + HttpUtility.UrlEncode("1")) ' I have created a Milestone although I don't really want to set milestones?
data.Append("&responsibles=" + HttpUtility.UrlEncode({MY User GUID - Obtained from an earlier query}))
data.Append("&notify=" + HttpUtility.UrlEncode("true"))
data.Append("&startDate=" + "2015-05-10")


postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)

If Not postStream Is Nothing Then postStream.Close()


Using response As WebResponse = request.GetResponse()
Using stream As Stream = response.GetResponseStream() ' ***** Bad request Here! 400
doc = XDocument.Load(stream)
End Using
End Using


Can you give me some help to advance beyond here please?

Many Thanks

Ken Grant
kengrant
 
Posts: 1
Joined: Sat May 09, 2015 11:37 am


Return to API

Who is online

Users browsing this forum: No registered users and 3 guests