documet upload in REST c# is giving 401 un-authirized error
the following code in c# in auto hosted app upload document giving 401 unauthorized error. please let me know.
httpwebrequest endpointrequest = (httpwebrequest)httpwebrequest.create(hostweb + "/_api/web/getfolderbyserverrelativeurl('shared%20documents'/files/add(url='" + filename + "',overwrite=true))");
endpointrequest.method = "post";
endpointrequest.accept = "application/json;odata=verbose";
endpointrequest.headers.add("authorization", "bearer" + accesstoken);
endpointrequest.headers.add("binarystringrequestbody", "true");
var reqstream = endpointrequest.getrequeststream();
reqstream.write(bytefile, 0, bytefile.length);
reqstream.flush();
reqstream.close();
httpwebresponse endpointresponse = (httpwebresponse)endpointrequest.getresponse();
assuming accesstoken valid, found couple typos/syntax errors in code:
- should be: /web/getfolderbyserverrelativeurl('shared%20documents')/files/add(url='" + filename + "',overwrite=true) -- didn't close parenthesis after shared%20documents , had parenthesis @ end
- should be: "bearer " + accesstoken -- didn't have space after bearer
the following code worked me in autohosted app:
httpwebrequest endpointrequest = (httpwebrequest)httpwebrequest.create(hostweb + "/_api/web/getfolderbyserverrelativeurl('shared%20documents')/files/add(url='" + filename + "',overwrite=true)"); endpointrequest.method = "post"; endpointrequest.headers.add("binarystringrequestbody", "true"); endpointrequest.headers.add("authorization", "bearer " + accesstoken); endpointrequest.getrequeststream().write(bytefile, 0, bytefile.length); httpwebresponse endpointresponse = (httpwebresponse)endpointrequest.getresponse();
please mark reply helpful (the arrow) if useful , please mark answer (the check box) if answered question! thank you!
danny jessee | mcpd - sharepoint developer 2010 | mcts - sharepoint 2010, configuring
blog: http://dannyjessee.com/blog | twitter: @dannyjessee
SharePoint , Apps for Office and SharePoint > Developing Apps for SharePoint 2013
Comments
Post a Comment