Bitbucket API Invoke-RestMethod,Powershell,获取数据到Json文件,webscarping

时间:2016-01-08 20:37:34

标签: json

我尝试做与Bibucket API解释完全相同的事情

BitBucket说/解释

https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html

获取https://api.bitbucket.org/2.0/repositories/ {owner} / {repo_slug}

{
    "scm": "hg",
    "has_wiki": true,
    "description": "Site for tutorial101 files",
    "links": {
        "watchers": {
            "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/watchers"
        },
        "commits": {
            "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/commits"
        },
        "self": {
            "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org"
        },
        "html": {
            "href": "https://bitbucket.org/tutorials/tutorials.bitbucket.org"
        },
        "avatar": {
            "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2012/Nov/28/tutorials.bitbucket.org-logo-1456883302-9_avatar.png"
        },
        "forks": {
            "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/forks"
        },
        "clone": [{
            "href": "https://bitbucket.org/tutorials/tutorials.bitbucket.org",
            "name": "https"
        }, {
            "href": "ssh://hg@bitbucket.org/tutorials/tutorials.bitbucket.org",
            "name": "ssh"
        }],
        "pullrequests": {
            "href": "https://api.bitbucket.org/2.0/repositories/tutorials/tutorials.bitbucket.org/pullrequests"
        }
    },
    "fork_policy": "allow_forks",
    "name": "tutorials.bitbucket.org",
    "language": "html/css",
    "created_on": "2011-12-20T16:35:06.480042+00:00",
    "full_name": "tutorials/tutorials.bitbucket.org",
    "has_issues": true,
    "owner": {
        "username": "tutorials",
        "display_name": "tutorials account",
        "uuid": "{c788b2da-b7a2-404c-9e26-d3f077557007}",
        "links": {
            "self": {
                "href": "https://api.bitbucket.org/2.0/users/tutorials"
            },
            "html": {
                "href": "https://bitbucket.org/tutorials"
            },
            "avatar": {
                "href": "https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png"
            }
        }
    },
    "updated_on": "2014-11-03T02:24:08.409995+00:00",
    "size": 76182262,
    "is_private": false,
    "uuid": "{9970a9b6-2d86-413f-8555-da8e1ac0e542}"
}

我想使用powershell做同样的事情。

所以,我做了一个代码。

$username = ""
$password = ""

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$response=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri https://bitbucket.org/susco/azuretoquickbase -Method Get

$response

我确信我可以登录bitbucket,因为我看到" $性反应的"

然后,就像Bitbucket所说,我添加了这样的代码来回显Json文件。

 $username = ""
$password = ""

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$response=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri https://bitbucket.org/susco/azuretoquickbase -Method Get

$response


$json = $response.Content|ConvertFrom-Json


$json.data.summaries

但它导致错误

ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At line:11 char:27
+ $json = $response.Content|ConvertFrom-Json
+                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

关键是我确信我可以登录并进入bitbucket所说的页面,但它没有得到json文件和null错误。

我想要像Bitbuckets页面一样回应,我该怎么做?

1 个答案:

答案 0 :(得分:2)

public function getUnique32() { $session = $this->request->session(); if (!$session->check('unique32')) { $unique32 = 0; echo "unique32 starts at 0"; } else { $unique32 = $session->read('unique32'); echo "unique32 is $unique32"; } if (++$unique32 >= 0xffffffff) { $unique32 = 0; echo "unique32 is reset to 0"; } $session->write('unique32', $unique32); return $unique32; } 会自动将JSON结果转换为对象。如果您只想要原始结果,请使用Invoke-RestMethod并获取内容属性。

或者在您的情况下,因为您似乎想要该对象,只需使用Invoke-WebRequest

相关问题