Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication 带有HTTP基本身份验证的PhoneGap文件传输_Authentication_Cordova - Fatal编程技术网

Authentication 带有HTTP基本身份验证的PhoneGap文件传输

Authentication 带有HTTP基本身份验证的PhoneGap文件传输,authentication,cordova,Authentication,Cordova,我正在尝试使用将文件从PhoneGap上载到服务器。我需要启用此上载的HTTP基本身份验证 以下是相关代码: var options = new FileUploadOptions({ fileKey: "file", params: { id: my_id, headers: { 'Authorization': _make_authstr() } } }); var ft

我正在尝试使用将文件从PhoneGap上载到服务器。我需要启用此上载的HTTP基本身份验证

以下是相关代码:

    var options = new FileUploadOptions({
        fileKey: "file",
        params: {
            id: my_id,
            headers: { 'Authorization': _make_authstr() }
        }
    });
    var ft = new FileTransfer();
    ft.upload(image, 'http://locahost:8000/api/upload', success, error, options);
似乎我可以通过在“params”列表中包含“headers”来指定授权标头,就像我在上面所做的那样:

      JSONObject headers = params.getJSONObject("headers");
      for (Iterator iter = headers.keys(); iter.hasNext();)
      {
        String headerKey = iter.next().toString();
        conn.setRequestProperty(headerKey, headers.getString(headerKey));
      }
然而,这似乎并没有实际添加标题


那么:对于iPhone和Android,有没有一种方法可以通过PhoneGap的文件传输进行HTTP基本身份验证?

您可以通过将自定义头添加到选项中,而不是像这样添加到参数中:

authHeaderValue = function(username, password) {
    var tok = username + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
};

options.headers = {'Authorization': authHeaderValue('Bob', '1234') };

headers数组的正确位置是作为选项的直接子级。选项->标题。不是选项->参数->标题。以下是一个例子:

//**************************************************************
//Variables used below:
//1 - image_name: contains the actual name of the image file.
//2 - token: contains authorization token. In my case, JWT.
//3 - UPLOAD_URL: URL to which the file will be uploaded.
//4 - image_full_path - Full path for the picture to be uploaded.
//***************************************************************
var options = {
  fileKey: "file",
  fileName: 'picture',
  chunkedMode: false,
  mimeType: "multipart/form-data",
  params : {'fileName': image_name}
};

var headers = {'Authorization':token};

//Here is the magic!
options.headers = headers;
//NOTE: I creaed a separate object for headers to better exemplify what
// is going on here. Obviously you can simply add the header entry
// directly to options object above.

$cordovaFileTransfer.upload(UPLOAD_URL, image_full_path, options).then(
   function(result) {
      //do whatever with the result here.
 });

以下是官方文档:

您可以自己创建授权标头。但您也可以在url中输入凭据,如下所示:

var username = "test", password = "pass";     
var uri = encodeURI("http://"+username + ':' + password +"@localhost:8000/api/upload");
请参见FileTransfer.js了解实现(第45行):


对于任何想知道的人来说,上面列出的这个方法对我很有效。只需添加以下内容:
params.headers={Authorization:'Basic'+creds};options.params=参数标题需要转到options.headers,而不是添加到options.params。仅供参考,我在iOS中对此有问题(从2013年1月14日起)。在Android和BB中运行良好…您使用的是什么版本的phonegap?我在iOS上成功地使用了2.3.02.2.0。。。这很有趣,也许我应该尝试升级?你以前在2.2.0版本中遇到过问题吗?是的,但是我也(错误地)在操作时将头放在参数中。哦,我也在这样做:
params.headers={Authorization:'Basic'+loginCreds}这不对吗?
function getBasicAuthHeader(urlString) {
var header =  null;


// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem

if (window.btoa) {
    var credentials = getUrlCredentials(urlString);
    if (credentials) {
        var authHeader = "Authorization";
        var authHeaderValue = "Basic " + window.btoa(credentials);

        header = {
            name : authHeader,
            value : authHeaderValue
        };
    }
}

return header;
}