powershell http post REST API basic authentication

后端 未结 4 556
甜味超标
甜味超标 2020-12-13 20:17

I have basic authentatication working with REST API using curl:

curl -X POST  -H \'Accept: application/json\' -u user:password http://localhost/test/
         


        
相关标签:
4条回答
  • 2020-12-13 20:28

    I know this is an old thread, but for those who might stumble across this the invoke-restmethod is a much better, and simpler, vehicle for making API calls with PowerShell.

    Build a parameter list as a hash table:

    $params = @{uri = 'https:/api.trello.com/1/TheRestOfYourURIpath';
                       Method = 'Get'; #(or POST, or whatever)
                       Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"));
               } #end headers hash table
       } #end $params hash table
    
    $var = invoke-restmethod @params
    

    Your parameter hash table may differ slightly.

    I actually haven't gotten this to work with Trello, but I have with GitHub, Serena Business Manager and Jira.

    0 讨论(0)
  • 2020-12-13 20:28

    Credentials property seems to be used for Windows authentication. Try using this function: Forcing Basic Authentication in WebRequest I would advise you in any case to use some web debugger, like Fiddler to see the difference between curl request and your request

    0 讨论(0)
  • 2020-12-13 20:43

    This is the code I use to download pages from Confluence as HTML files.

    $pageid = "176398584" ;
    $url = "http://wikiserver/wiki/pages/viewpage.action?pageId=$pageid" ;
    write-host "Establish credentials" ;
    $r = Invoke-WebRequest "http://wikiserver/wiki/pages/login.action" -SessionVariable my_session ;
    # $r ;
    $form = $r.Forms[1]; 
    # $form ; 
    
    # $c = $host.UI.PromptForCredential('Your Credentials', 'Enter Credentials', '', '') ;
    # $form.fields['os_username'] = $c.UserName ;
    # $form.fields['os_password'] = $c.GetNetworkCredential().Password ;
    $form.fields['os_username'] = "mywikirobotlogonname" ;
    $form.fields['os_password'] = "mywikirobotpassword"  ;
    $form.fields['os_cookie']      = "true" ; 
    $form.fields['os_destination'] = "%2Fpages%2Fviewpage.action%3FpageId%3D$pageid" ; 
    
    $outputFile = "$pageid.html" ;
    $content = Invoke-WebRequest -Uri ($url + $form.Action)  -WebSession $my_session -Method POST -Body $form.Fields ;
    $content.ParsedHTML.getElementById("content").innerHTML | Add-Content $outputFile
    

    The host UI Prompted can be used to ask the user to enter their logon information.

    Uncomment a variable to display to the system output the contents of the form, the retrieved page, etc to troubleshoot- $r $form $content

    0 讨论(0)
  • 2020-12-13 20:45

    Your code looks good, I would try adding HTTP_AUTHORIZATION header for $webrequest like this:

    $webRequest.Headers.Add("AUTHORIZATION", "Basic YTph");
    

    Where YTph would be the base64encoded string for username : password.

    0 讨论(0)
提交回复
热议问题