如何通过用户名和apikey到restfull web api来获取身份验证令牌 [英] how to get authenticate token by pass username and apikey to restfull web api

查看:143
本文介绍了如何通过用户名和apikey到restfull web api来获取身份验证令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨开发人员我是ajax的新手,第一次使用web api。

这是我的json对象来验证用户。我如何通过ajax调用获取身份验证令牌请帮助我

隐藏复制代码



Hi developers I am new in ajax and first time consuming web api .
This is my json object to authenticate a user . how can i pass this in ajax call to get the authenticate token please help me
Hide Copy Code

POST /v2.0/tokens HTTP/1.1
User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
Host: identity.api.rackspacecloud.com
Accept: application/json
Content-Type: application/json
Content-Length: 54
 
{
   "auth":
   {
      "RAX-KSKEY:apiKeyCredentials":
      {
         "username": (1)"jsmith",
         "apiKey": (2)"aaaaa-bbbbb-ccccc-12345678"
      }
   }
}



我的要求是我想将这个json对象发布到服务器并从中获取响应服务器,但功能总是返回错误条件我要求google多次这个并找到了很多解决方案并尝试了所有这些,但我仍然无法发布数据

我这样做但不能消耗请给我一些解决方案




My requirement is that I want to Post this json object to server and get the response from server but function always returns error condition I asked to google about this many times and found lots of solution and tried all those but still I am not able to post data
I am doing this but not able to consume please give me some solution

$.ajax({
                    url: "https://identity.api.rackspacecloud.com/v1.0/tokens",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Content-Type: application/json");
                    },
                    type: 'POST',
                    dataType: 'json',
                    accept:'application/json',
                    
                 //  processData: true,
                    data: JSON.stringify({ "auth": { "RAX-KSKEY:apiKeyCredentials": { username: 'jsmith', apiKey: 'aaaaa-bbbbb-ccccc-12345678' } } }),
                    success: function (response) {
                        alert(JSON.stringify(response));
                    },
                    error: function () {
                        alert("Cannot get data");
                    }
                });

推荐答案

.ajax({
url: https://identity.api.rackspacecloud.com/v1.0/tokens
beforeSend: function (xhr){
xhr.setRequestHeader( Content-Type:application / json);
},
类型:' POST'
dataType:' json'
接受:' application / json'

// processData:true,
data: JSON .stringify({ auth:{ RAX-KSKEY:apiKeyCredentials:{username: ' jsmith',apiKey:' < span class =code-string> aaaaa-bbbbb-ccccc-12345678'}}}),
成功: function (响应) {
alert( JSON .stringify(response));
},
错误: function (){
alert( 无法获取数据);
}
});
.ajax({ url: "https://identity.api.rackspacecloud.com/v1.0/tokens", beforeSend: function (xhr) { xhr.setRequestHeader("Content-Type: application/json"); }, type: 'POST', dataType: 'json', accept:'application/json', // processData: true, data: JSON.stringify({ "auth": { "RAX-KSKEY:apiKeyCredentials": { username: 'jsmith', apiKey: 'aaaaa-bbbbb-ccccc-12345678' } } }), success: function (response) { alert(JSON.stringify(response)); }, error: function () { alert("Cannot get data"); } });






怎么做你生成apiKey?你确定密钥仍然有效吗?如果你知道这些问题的答案,那么问题的一半就解决了。



当您发送身份验证详细信息时,您必须将其作为请求标头的一部分发送而不是数据。从您提供的代码我假设您正在使用基本身份验证。



但是,您知道这些凭据很容易被泄露吗?因为这些是base64编码未加密的。选择身份验证方法时请考虑到这一点。



以下是一些文章,介绍如何在请求标头中发送身份验证详细信息。



http:// stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax [ ^ ]



< a href =http://blog.tompawlak.org/basic-authentication-jquery-ajax> http://blog.tompawlak.org/basic-authentication-jquery-ajax [ ^ ]



http://laravel.io/forum/12-11-2014- basic-auth-api-jquery-cors [ ^ ]



试试这些,看看如果有效。
Hi,

How did you generate apiKey? Are you sure that key is still valid? If you know the answer to these questions, half of the problem is solved.

When you send the authentication details, you have to send it as part of the Request Header, not as data. From the code you provided I assume you are using basic authentication.

But, do you know that these credentials can easily be compromised? Because these are base64 encoded not encrypted. Do take that into consideration when you choose a authentication method.

Here are some articles that show how to send the authentication details in request header.

http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax[^]

http://blog.tompawlak.org/basic-authentication-jquery-ajax[^]

http://laravel.io/forum/12-11-2014-basic-auth-api-jquery-cors[^]

Try these and see if it works.


这篇关于如何通过用户名和apikey到restfull web api来获取身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆