Angular 5 OAuth身份验证错误 - “意外的输入结束”

时间:2018-01-07 03:51:00

标签: angular rest http oauth

我有一个C#REST Api在使用Postman进行OAuth身份验证时使用相同的值,但是使用Angular我从控制台获得“SyntaxError:意外的输入结束”。我错过了什么?

loginUser(username: string, password: string) {
    const bodyData = {
      grant_type: 'password',
      username: username,
      password: password,
      myCustomData: 'Hi There'
    };
    const urlSearchParams = new URLSearchParams();
    Object.keys(bodyData).forEach(key => urlSearchParams.append(key, bodyData[key]));

    const headers = new Headers();
    headers.append('Accept', 'application/json;charset=UTF-8');
    headers.append('Content-Type', 'application/x-www-form-urlencode;charset=UTF-8' );
    const options = new RequestOptions({ headers: headers });

    return this.http.post('http://localhost:19483/api/token', urlSearchParams, options)
      .map(response => {
        const retVal = response.json();
          return {
            'access_token': retVal.access_token,
            '.expires': retVal['.expires'],
            'userId': retVal.userId,
            'roles': retVal.roles
        };
      })
      .do(res => {
        if (res) {
          window.localStorage.setItem('access_token', res.access_token);
          window.localStorage.setItem('roles', res.roles);
          return res;
        }
      })
      .catch(error => {
        return Observable.of(false);
      });
  }

0 个答案:

没有答案