使用OAuth2访问WD Personal MyCloud NAS

时间:2017-04-17 04:59:01

标签: c# php android oauth-2.0

我是Android应用中的新手。我正在开发一个可以使用OAuth2.o访问位于我家的个人西部数据云的应用程序。我使用的是xamarin C#。我在网上搜索了一个月,我找不到任何东西。我已经拥有Western Digital的ClientID和Client Secret。他们有这个例子:`

<?php
$endpoint = "https://idp.mycloud.com/as/authorization.2";

$authCodeParams = array(
"client_id" => "ZnViYXJhcmV5b3VmdXVk",
"client_secret" => "QPc7J9LnrwWjbTZvbuNiWS59sfb+gBwYHPGLW/QDHlo=",
"response_type" => "code"
"redirect_uri" => "http://localhost"
);

$authCode = getAuthCode($authCodeParams)

function getAuthCode($params) {

$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type:
application/x-www-form-urlencoded');

//If ssl validation fails, try un-commenting this line
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$postData = "";

//create body
foreach($params as $k => $v) {
   $postData .= $k . '='.urlencode($v).'&';
}

$postData = rtrim($postData, '&');

curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status != 200) {
  throw new Exception("Auth code request to $endpoint failed. 
Status:  $status. Response: $response. curl_error " .
                      curl_error($curl) .
                      ", curl_errno " .
                      curl_errno($curl) . "\n");
}
curl_close($curl);

//extract redirect from response Location header
$headers = explode("\n", $response);
foreach($headers as $header) {
   if (strpos($header, "Location:") !== false) {
       $redirectUrl = trim(str_replace("Location:","",$header));
       break;
   }
}

//extract auth code from redirect response
$authCode = ""
if (isset($redirectUrl)) {
    //parse auth-code from url
    $query = parse_url($redirectUrl, PHP_URL_QUERY);
    parse_str($query, $params);
    if (isset($params['code'])) {
       $authCode = $params['code'];
    }
}

return $authCode;
}
?>'

问题是,PHP不是C#,请帮忙吗?谢谢。 您可以看到整个code here`

`

0 个答案:

没有答案