浙里办 单点登录之获取token

C#代码

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Security.Cryptography;

class Program
{
    static void Main()
    {
        string accessKey = "你的ak";
        string secret = "你的sk";
        string path = "/restapi/prod/IC33000020220329000007/uc/sso/access_token";
        string queryString = ""; // 如果有查询参数,这里应该是URL编码后的查询字符串
        string time = DateTime.UtcNow.ToString("r"); // GMT格式的时间

        // 构建待签名的字符串
        string signString = "POST\n" + path + "\n" + queryString + "\n" + accessKey + "\n" + time + "\n";

        // 使用HMAC SHA256进行签名
        byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
        byte[] signStringBytes = Encoding.UTF8.GetBytes(signString);
        string signature;
        using (var hmac = new HMACSHA256(secretBytes))
        {
            byte[] hash = hmac.ComputeHash(signStringBytes);
            signature = Convert.ToBase64String(hash);
        }

        // 准备POST数据  把xxxxxx换成你的appId
        string postData = "{\"ticketId\":\"debug_tk_e4a0dc3fcc8d464ba336b9bcb1ba2072\",\"appId\":\"xxxxxx\"}";
        byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);

        // 创建HttpWebRequest对象
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://ibcdsg.zj.gov.cn:8443" + path);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Headers.Add("X-BG-HMAC-ALGORITHM", "hmac-sha256");
        request.Headers.Add("X-BG-HMAC-ACCESS-KEY", accessKey);
        request.Headers.Add("X-BG-DATE-TIME", time);
        request.Headers.Add("X-BG-HMAC-SIGNATURE", signature);
        request.ContentLength = postDataBytes.Length;

        // 写入POST数据
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(postDataBytes, 0, postDataBytes.Length);
        }

        // 获取响应
        string responseContent = "";
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            responseContent = reader.ReadToEnd();
        }

        Console.WriteLine(responseContent);
    }
}

PHP代码

<?php

$accessKey = "你的ak";
$secret = "你的sk";
$time = gmdate('D, d M Y H:i:s T'); // GMT格式的时间
$queryString = ""; // 如果是GET方法,且有查询参数,这里应该是URL编码后的查询字符串
$path = '/restapi/prod/IC33000020220329000007/uc/sso/access_token';

// 构建待签名的字符串
$signString = "POST" . "\n" . $path . "\n" . $queryString . "\n" . $accessKey . "\n" . $time . "\n";

// 使用HMAC SHA256进行签名
$hash = hash_hmac('sha256', $signString, $secret, true);

// 将结果转换为Base64
$signature = base64_encode($hash);

// 准备POST数据和HTTP头部
$url = 'https://ibcdsg.zj.gov.cn:8443' . $path; // 目标URL
$postData = array(
    'ticketId' => 'debug_tk_e4a0dc3fcc8d464ba336b9bcb1ba2072',
    'appId' => '你的appId'
);
$postDataJson = json_encode($postData);

$headers = array(
    'X-BG-HMAC-ALGORITHM: hmac-sha256',
    'Content-Type: application/json',
    'X-BG-HMAC-ACCESS-KEY: ' . $accessKey,
    'X-BG-DATE-TIME: ' . $time,
    'X-BG-HMAC-SIGNATURE: ' . $signature,
);

// 发送请求(使用cURL)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson); // 发送JSON数据
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);

echo $response;

posted @ 2023-12-30 15:21  KittySmith  阅读(147)  评论(0编辑  收藏  举报