如何正确设置使用Nativescript [2.5.4]使用Wordpress Rest API(Oauth1)的授权?

时间:2017-04-15 16:45:17

标签: nativescript angular2-nativescript wordpress-rest-api

我正在尝试使用Nativescript来使用WP Rest API。

  1. WP API和OAUTH1授权已经很好地设置并使用POSTMAN进行测试。
  2. Nativescript使用休息的登录功能也已设置,无需OAUTH。
  3. 现在我正在尝试使用OAUTH登录代码:

         authorizationString = 'OAuth  oauth_consumer_key="T2yXcbN28Ufj",
          oauth_token="AmEVr5oSNmbKyZKccFjtmnSk",
          oauth_signature_method="HMAC-SHA1",
          oauth_timestamp="1492267438",
          oauth_nonce="rGFJG2",
          oauth_version="1.0",
          oauth_signature="Ru%2BaSvsZn2liesd2ENy8GeNsdHY%3D"';
    
         login(user: User){
          console.log("Try to login User : " + user.email);
          let headers = new Headers({"Authorization": authorizationString});
          let body = JSON.stringify({
            username: user.username,
            email: user.email,
            password: user.password
          });
          headers.append("Content-Type", this.contentType);
    
          return this.http.post(
            this.api,
            body,
            {headers: headers}
          )
          .map( response => response.json() )
          .do(data => {
            //Do work!!
          })
          .catch(this.handleErrors);
       }
    

    但是我收到了一个错误,这个错误意味着 autorization格式不正确或发送

    "data": {
        "code": "rest_cannot_access",
        "message": "Only authenticated users can access the REST API.",
        "data": {
            "status": 401
        }
    }
    

    如何正确使用oauth1和Nativescript?

1 个答案:

答案 0 :(得分:0)

我刚刚使用开源插件切换到OAUTH2:

https://github.com/wlcdesigns/WP-OAuth2-Server-Client

使用authorization : Bearer

更容易使用