Skip to content

签名不过有遇到过吗 #7

@smalldarks

Description

@smalldarks
// 火山OCR
import { Signer } from '@volcengine/openapi';
import qs from 'querystring'
import axios from 'axios'

const accessKeyId = "xxxx"
const secretKey = "xxx=="

// @ts-ignore
function getAuth() {
  // http request data
  const openApiRequestData = {
    region: 'cn-north-1',
    method: 'POST',
    // [optional] http request url query
    params: {
      Action: 'OCRNormal',
      Version: '2020-08-26'
    },
    // http request headers
    headers: {
      // 'content-type': 'application/x-www-form-urlencoded',
      // 'host': 'open.volcengineapi.com'
    },
    // [optional] http request body
    body: "",
  }

  const signer = new Signer(openApiRequestData, "cv");

  // sign
  signer.addAuthorization({ accessKeyId, secretKey });

  // Print signed headers
  return openApiRequestData.headers
}

const pickUp = async(imgUrl:string):Promise<any> => {
  const auth = getAuth()
  const query = {
    Action: 'OCRNormal',
    Version: '2020-08-26'
  }
  
  try {
    const data = await axios({
      url: `https://visual.volcengineapi.com?${qs.stringify(query)}`,
      method: "post",
      data: {
        image_url: imgUrl
      },
      headers: {
        // ...form.getHeaders(),
        // 'content-type': 'application/x-www-form-urlencoded',
        ...auth
      }
    })
    return data;
  } catch (error) {
    console.log('error=======>', error)
  }
}

报签名不通过,究竟是啥原因
{
    "ResponseMetadata": {
        "RequestId": "2023112420563676611E13AC9BDCC73139",
        "Action": "OCRNormal",
        "Version": "2020-08-26",
        "Service": "cv",
        "Region": "cn-north-1",
        "Error": {
            "CodeN": 100010,
            "Code": "SignatureDoesNotMatch",
            "Message": "The request signature we calculated does not match the signature you provided. Check your Secret Access Key and signing method. Consult the service documentation for details."
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions