0
votes

I have a sharepoint list(User). I am able to get these data with simple http request in browser.here is the out put of request: enter image description here

all items retrieving as you see below and I am trying to achieve this in typescript service here is my code piece;

getUsers() : Observable<Users[]> {
        var headers = new Headers();
        headers.append('accept', 'application/json;odata=verbose');
         return this.http_.get("https://yeneryilmaz1-3200ef41d84877.sharepoint.com/Sharepoint-TS-Angular2/_api/web/lists/getByTitle('User')/items",{headers})
         .map((res:Response) => res.json())
         .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
     }

but it always throws exception.. how can I get list items via http request and also I know i need to add access token of from sharepoint account how can I add them to headers ?

EDITED: response of the server for this request in browser console: "GET https://yeneryilmaz1-***.sharepoint.com/Sharepoint-TS-Angular2/_api/web/lists/getByTitle('User')/items 403 (Forbidden)"

so it seems authentication issue i encounter, so.. how can I embed access tokens to this request

1

1 Answers

0
votes

You are very close. You need to add the authorization in the header too.

Make sure you are importing 'Headers' from http.

import { Http, Response, RequestOptionsArgs, Headers } from '@angular/http';        

The call:

getUserData(): Observable<IUsers[]> {
    var _headers = new Headers();

    _headers.append('Content-Type', 'application/json');
    _headers.append('Authorization', 'Bearer ' + access_token_here);

    return this._http.get(this._protocolUrl, {
        headers: _headers
    })
        .map((res: Response) => <IUser[]> res.json())
        .do(data => console.log('All: ' + JSON.stringify(data)))
}