class Rest{\n\n\tprotected $strUrl;\n\n\tprotected $strToken;\n\n\tprotected $strBody;\n\n\tpublic function requestToken(){\n\t\tif (!file_exists(TOKEN_STORAGE)){\n\t\t\techo 'Get Token from storage'.\"\\n\";\n\t\t\t$data = file_get_contents(TOKEN_STORAGE);\n\t\t\t$arrToken = json_decode($data);\n\t\t\tif ((filemtime(TOKEN_STORAGE) + $arrToken->expires_in) > time()) {\n\t\t\t\t$this->strToken = $arrToken->access_token;\n\t\t\t} else {\n\t\t\t\techo 'Token expired'.\"\\n\";\n\t\t\t\tunlink(TOKEN_STORAGE);\n\t\t\t\t$this->requestToken();\n\t\t\t}\n\t\t} else {\n\t\t\techo 'Get new token from API'.\"\\n\";\n\t\t\t$arrData = array(\n\t\t\t\t'grant_type=client_credentials',\n\t\t\t\t'client_id='.CLIENT_ID,\n\t\t\t\t'client_secret='.urlencode(ACCESS_KEY),\n\t\t\t\t'scope=urn%3aWindowsAzureMediaServices'\n\t\t\t);\n\t\t\t$arrHeader = array(\n\t\t\t\t'Content-length:'.strlen($this->generateData($arrData))\n\t\t\t);\n\n\t\t\t$ch = curl_init();\n\t\t\tcurl_setopt($ch, CURLOPT_URL, TOKEN_URL);\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateData($arrData));\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);\n\t\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n\t\t\t$data = curl_exec($ch);\n\t\t\tcurl_close($ch);\n\n\t\t\t$arrToken = json_decode($data);\n\t\t\tif (isset($arrToken->error)){\n\t\t\t\tprint_r($arrToken);\n\t\t\t\tdie();\n\t\t\t}\n\t\t\t$this->strToken = $arrToken->access_token;\n\t\t\tfile_put_contents(TOKEN_STORAGE, $data);\n\t\t\techo 'Token save in storage'.\"\\n\";\n\t\t}\n\t return $this->strToken;\n\t}\n}<\/pre>\nThe AMS response will contains the token. If you make many calls, I suggest to cache the access token, the expires_in value and the timestamp of the request to avoid unnecessary calls.<\/p>\n
Now, we have a token to access, we can play with the Azure Media Services. We will see it in next posts.<\/p>\n","protected":false},"excerpt":{"rendered":"
I open a serie of posts which will talk about this solution. Microsoft announced at the end of spring the preview of Windows Azure Media Services. WAMS form an extensible media platform that integrates the best of the Microsoft Media Platform and third-party media components in Windows Azure.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[57,48,46],"tags":[],"_links":{"self":[{"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/posts\/786"}],"collection":[{"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":31,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":1928,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/posts\/786\/revisions\/1928"}],"wp:attachment":[{"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alkannoide.com\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}