我可以访问我的私人资料库到位桶使用Ajax和jQuery? [英] Can I access my private bitbucket repository with ajax and jquery?

查看:116
本文介绍了我可以访问我的私人资料库到位桶使用Ajax和jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站,我目前生成的标记列表从我的仓库使用jQuery托管在bitbucket.org之一。但是,为了做到这一点,我不得不做出库公开。我想preFER保持它私有的。

是否有可能为我以允许我的程序存储库站点访问以这种方式,同时仍保持库私有

在code看起来像这样(在这种形式下,它会产生的所有标签的​​控制台列表)。

  $。阿贾克斯({
        网址:https://api.bitbucket.org/1.0/repositories/jeffreycwitt/publicrepository/tags
        数据类型:JSONP
        跨域:真正的,
        成功:函数(returndata){
           $每个(returndata,功能(键,值){
               执行console.log(钥匙)
    });
});
 

解决方案

基本上,我已经学会了一个授权头是必要的。而共识似乎是,这不能用jQuery的JSONP请求完成。我真的不知道为什么。

不过,我已经能够通过编写,通过一个PHP 的file_get_contents 通话passess授权头一个PHP脚本来达到预期的效果。然后由意见按照上面的建议,我可以使用AJAX脚本加载的愿望数据。 PHP脚本是这样的:

 背景= stream_context_create(阵列(
'HTTP'=>阵列(
    '头'=> 授权:基本。 base64_en code($用户名:密码$)
)
));

//使请求
$ JSON =的file_get_contents($网址,虚假,$背景);

//解析数据
//把JSON数据到一个数组
$ obj_a = json_de code($ json的,真正的);

//获取数组的所有键
$标签= array_keys($ obj_a);
 

因此​​,如果有人想从中检索到位桶专用存储库中所有的标签,这是你如何做到这一点。到位桶API文档不说任何东西如何到正宗除非卷曲手段的私人资料库。但添加标题是你需要的,如果你不使用卷曲的事情。

希望有所帮助别人。 (请随意编辑这个答案,如果你能想到你这更好的解释)。

On my site, I'm currently generating a list of tags from one of my repositories hosted on bitbucket.org using jquery. But in order to do this, I had make the repository public. I would prefer to keep it private.

Is it possible for to me to allow the site access to my respository in this way, while still keeping the repository private.

The code looks like this (in this form, it will produce a list in the console of the all tags).

$.ajax({
        url:"https://api.bitbucket.org/1.0/repositories/jeffreycwitt/publicrepository/tags",
        dataType: "jsonp",
        crossDomain: true,
        success: function (returndata){
           $.each(returndata, function(key, value){
               console.log(key)
    });
});

解决方案

Basically I've learned that an "Authorization header is needed." And the consensus seems to be that this cannot be done with JSONP requests in jquery. I don't really know why.

But I have been able to achieve the desired result by writing a php script that passess the authorization header through a php file_get_contents call. Then as suggested by the comments above, I can use the ajax script to load the desire data. The php script looks like this:

context = stream_context_create(array(
'http' => array(
    'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));

// Make the request 
$json = file_get_contents($url, false, $context);

//parse data
//turn json data into an array
$obj_a = json_decode($json, true);

//get all keys of array
$tags = array_keys($obj_a);

So if anyone wants to retrieve all the tags from a private bitbucket repository, this is how you do it. Bitbucket api documentation doesn't saying anything about how to authentic into a private repository except by means of CURL. But adding the header is what you need to do if you are not using CURL.

Hope that helps someone. (Please feel free to edit this answer if you can think you explain this better).

这篇关于我可以访问我的私人资料库到位桶使用Ajax和jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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