none
Problem for using REST api to get Assets list(Media service)

    Question

  • Hi,

    I followed the articles below to implement AMS REST api on our platform.

    I started to try to get Assets list(PHP source code is at the bottom), however could not get the list. 

    # reference articles

    - Access the Azure Media Services API with Azure AD authentication

    https://docs.microsoft.com/en-us/azure/media-services/media-services-use-aad-auth-to-access-ams-api

    - Use Azure AD authentication to access the Azure Media Services API with REST

    https://docs.microsoft.com/en-us/azure/media-services/media-services-rest-connect-with-aad

    - Azure Media Service REST API

    https://docs.microsoft.com/en-us/rest/api/media/operations/asset#list_an_asset

    I tried the example here, couldn't get Assets list too.

    Besides, is there any example about REST api but not use POSTMAN? (ex, using cURL)

    It would be great hear any from you.

    Thanks in advance,

    Ying

    ## php source code

    <?php

    // 認証, TOKEN取得
    $clientId = "b4fa****-****-****-****-****22ed";
    $clientKey = "AruY*****************=";
    $subscription = "85a7****-****-****-****-****ab26";
    $tenantId =  "20d1****-****-****-****-****4cf4";

    // AMS
    $tenant =  "xxxx.onmicrosoft.com";
    $amsClientId = "7057****-****-****-****-****c4ad";
    $amsClientKey = "tB+v************************=";
    $restApiEndpoint = "https://********.restv2.japaneast.media.azure.net/api";

    $cmd = "curl -X POST https://login.microsoftonline.com/{$tenant}/oauth2/token -F grant_type=client_credentials  -F client_id={$amsClientId} -F client_secret={$amsClientKey}";
    exec($cmd, $output, $result);

    $restToken = json_decode($output[0]);
    $method = "GET";

    $restToken = "{$restToken->token_type} {$restToken->access_token}";
    echo "{$restToken}";

    $uri = "{$restApiEndpoint}/Assets";
    $result = curlExcu($uri, $method, $restToken);

    function curlExcu($uri, $method, $token) {

        $ret  = "";

        $headers = [
            "x-ms-version:2.15",
            "Accept: application/json",
            "Content-Type: application/json",
            "DataServiceVersion: 3.0",
            "MaxDataServiceVersion: 3.0",
            "Authorization: {$token}"
        ];
        $options = [
            CURLOPT_URL            => $uri,
            CURLOPT_CUSTOMREQUEST  => $method,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER     => $headers
        ];

        try {
            $curl = curl_init($uri);

            $opt = curl_setopt_array($curl, $options);

            $result = curl_exec($curl);

            $ret = json_decode($result);
            curl_close($curl);
        } catch(Exception $e) {
            echo "error ====";
            print_r($e);
            $ret = null;
        }

        return $ret;

    }

    Thursday, March 8, 2018 4:18 AM

All replies

  • If you are using our sample Postman collection here - https://docs.microsoft.com/en-us/azure/media-services/postman-collection 

    You can hit the "code" button and get back both cURL and PHP sample code (at least on the Windows version you can - not sure about the Mac version of Postman).

    The URL endpoint that you should use for all REST API calls is the one that is given to you in the API Access blade of the Azure Portal, after you select Service Principal or User Auth.  The REST API URL is unique to each account and takes the format of:
    https://<accountname>.restv2.<location>.media.azure.net/api/

    To list all assets in an account you would call:
    https://<accountname>.restv2.<location>.media.azure.net/api/Assets

    To get a specific asset in an account you would call

    https://<accountname>.restv2.<location>.media.azure.net/api/Assets/{{assetId}}

    An example cURL post to list all assets in an account would look like this:

    curl -X GET \
      https://<<accountname>>.restv2.<<location>>.media.azure.net/api//Assets \
      -H 'Accept: application/json' \
      -H 'Cache-Control: no-cache' \
      -H 'Content-Type: application/json' \
      -H 'DataServiceVersion: 3.0' \
      -H 'MaxDataServiceVersion: 3.0' \
      -H 'Postman-Token: 3223378e-b9d5-bb80-d25d-5d79bb99e5f3' \
      -H 'User-Agent: azure media services postman collection' \
      -H 'x-ms-version: 2.18'

    An example PHP cURL request would look like this:

    <?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://<<accountname>>.restv2.<<location>>.media.azure.net/api//Assets",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "Accept: application/json",
        "Cache-Control: no-cache",
        "Content-Type: application/json",
        "DataServiceVersion: 3.0",
        "MaxDataServiceVersion: 3.0",
        "Postman-Token: bed3c5ff-f926-42f5-cd1e-b1379b25b325",
        "User-Agent: azure media services postman collection",
        "x-ms-version: 2.18"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }

    Hope that helps!

    Thanks,

    John Deutscher



    Thursday, March 8, 2018 10:29 PM
    Owner
  • Hi John,

    Thanks!

    From POSTMAN, I am good to get Assets list, and able to get the CODE.

    PHP works good too!

    thanks again,

    Ying


    • Edited by dci-cy Friday, March 9, 2018 5:16 AM
    Friday, March 9, 2018 3:41 AM