Вопрос

I'm attempting to obtain an access token for Microsoft's Translator API, and it's been quite a struggle. I first tried to write the request with Ruby and HTTParty, but it wasn't accepting my parameters. Microsoft provided an example in PHP, so I figured I could just use that:

http://msdn.microsoft.com/en-us/library/hh454950.aspx#phpexample

The code was copied directly from the Microsoft website--I just added in my variables and called the function at the end:

<?php

/*
 * Get the access token.
 *
 * @param string $grantType    Grant type.
 * @param string $scopeUrl     Application Scope URL.
 * @param string $clientID     Application client ID.
 * @param string $clientSecret Application client ID.
 * @param string $authUrl      Oauth Url.
 *
 * @return string.
 */
function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){
    try {
        //Initialize the Curl Session.
        $ch = curl_init();
        //Create the request Array.
        $paramArr = array (
             'grant_type'    => $grantType,
             'scope'         => $scopeUrl,
             'client_id'     => $clientID,
             'client_secret' => $clientSecret
        );
        //Create an Http Query.//
        $paramArr = http_build_query($paramArr);
        //Set the Curl URL.
        curl_setopt($ch, CURLOPT_URL, $authUrl);
        //Set HTTP POST Request.
        curl_setopt($ch, CURLOPT_POST, TRUE);
        //Set data to POST in HTTP "POST" Operation.
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
        //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //Execute the  cURL session.
        $strResponse = curl_exec($ch);
        //Get the Error Code returned by Curl.
        $curlErrno = curl_errno($ch);
        if($curlErrno){
            $curlError = curl_error($ch);
            throw new Exception($curlError);
        }
        //Close the Curl Session.
        curl_close($ch);
        //Decode the returned JSON string.
        $objResponse = json_decode($strResponse);
        if ($objResponse->error){
            throw new Exception($objResponse->error_description);
        }
        $finalresponse = $objResponse->access_token;
        return $finalresponse;

    } catch (Exception $e) {
        echo "Exception-".$e->getMessage();
    }

}

    $grant = "client_credentials"
$scope = "http://api.microsofttranslator.com";
$secret = "8Bo0SET1W8lmDgfV/l7FLEKUWRDXCEZrvTgv9tSH3Bs=";
    $client = "globalchatapp"
$auth = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";


getTokens($grant, $scope, $client, $secret, $auth);


?>

Anyway, when I run this (using PHP 5.3.6) there are no errors; it doesn't return any value. I'm not great with PHP, and it could be something glaringly obvious that I just haven't seen, but I've struggled with it for awhile to no avail. Any help would be greatly appreciated.

Also, if this helps, here was my original Ruby code. I did receive a response from the server here, saying 'scope' was missing:

require 'httparty'
scope = "http://api.microsofttranslator.com";
secret = "8Bo0SET1W8lmDgfV/l7FLEKUWRDXCEZrvTgv9tSH3Bs=";
auth = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
client_id = "globalchatapp"
grant_type = "client_credentials"

response = HTTParty.post("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/?&scope=#{scope}&secret=#{secret}&client_id=#{client_id}&grant_type=#{grant_type}", :body => {:key => :value})
puts response

Thanks again!

Это было полезно?

Решение

Store the value returned by getTokens() in a variable and pass it to urldecode() to decode the %2F and other encoded characters.

$tokens = getTokens($grant, $scope, $client, $secret, $auth);
echo urldecode($tokens);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top