0

I'm having trouble running DevOps API's via 'az rest' The two methods I attempted return the same result

The code snippet I'm working with

$resource = "https://graph.microsoft.com"
$uribase = "https://dev.azure.com/{org}/{project}"
$requestpath = "/_apis/build/builds?api-version=6.0-preview.6"
$uri = $uribase+$requestpath
az rest --uri $uri --headers "Content-Type=application/json" --resource $resource

I've tried several different API versions down to 5.1 but the result is the same. If i remove the --resource parameter it complains about not being able to determine the authentication. Is 'https://graph.microsoft.com' the right resource for this call?

The error returned

> az : Not a json response, outputting to stdout. For binary data suggest use "--output-file" to 
write to a file
At line:5 char:1
+ az rest --uri $uri --headers "Content-Type=application/json" --resour ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (Not a json resp...write to a file:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError 
The command failed with an unexpected error. Here is the traceback:
'charmap' codec can't encode character '\u221e' in position 6302: character maps to <undefined>
Traceback (most recent call last):
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site- 
packages\azure\cli\command_modules\util\custom.py", line 18, in rest_call
  File "C:\Users\VSSADM~1\AppData\Local\Temp\pip-install-yh2ypeu1\requests\requests\models.py", line 
897, in json
  File "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\lib\json\__init__.py", line 354, in loads
  return _default_decoder.decode(s)
....

I've written the output to a file. It's an HTML file that is related to sign in/sign out - nothing obvious from the text, but kindof a clue. As a result, I've been attempting to explicitly login to devops (even though I am logged in via 'az login') like this:

az devops login --organization https://dev.azure.com/{org}

The login hangs every time, I have to shut down powershell to restart. I've rebooted my machine, tried with and without the organization parameter, tried re-logging in via az login which works fine, but I cannot get the devops login command to work.

I'm not sure if the login is the issue or if its something else. Anyone else seen a situation like this? Thoughts on what to try?

Carlos
  • 128
  • 5
user2503078
  • 737
  • 1
  • 8
  • 24
  • `az devops login` is used personal access token to log in:https://learn.microsoft.com/en-us/azure/devops/cli/log-in-via-pat?view=azure-devops&tabs=windows – Jim Xu Aug 20 '20 at 08:00
  • Hi, thanks, i set my PAT via $env:AZURE_DEVOPS_EXT_PAT = '3aggid....' and tried logging in but it hangs. I am able to use CLI DevOps commands (az pipelines, etc.) so it's only the REST interface that fails. – user2503078 Aug 20 '20 at 10:56

1 Answers1

0

I've untangled most of this mess, hopefully someone else will benefit from this.

First, the hang. I never fixed this so it may still be related to the workaround I discuss below.

Fix #1: Instead of passing --resource, I punted on that and pass an authorization token in the header. The "/_apis/build/builds?api-version=6.0-preview.6" now the request method works with this authorization.

Fix #2: I was working with two different REST requests, one retrieves a simple list of builds, the other retrieves a simple list of release definitions. The base url is different for the release definition API requests, it requires 'vsrm.dev.azure.com' (i have no idea why).

$tokeninfo = az account get-access-token | convertfrom-json
$token = $tokeninfo.accessToken

$uribase = "https://vsrm.dev.azure.com/{org}/{project}"
$requestpath = "/_apis/release/definitions?api-version=6.0-preview.4"
$uri = $uribase+$requestpath
$authheader = "Authorization=Bearer " + $token
az rest --uri $uri --headers $authheader

$uribase = "https://dev.azure.com/{org}/{project}"
$requestpath = "/_apis/build/builds?api-version=6.0-preview.6"
$uri = $uribase+$requestpath
$authheader = "Authorization=Bearer " + $token
az rest --uri $uri --headers $authheader

Hope this helps someone out

user2503078
  • 737
  • 1
  • 8
  • 24