zoukankan      html  css  js  c++  java
  • OpenAPI系列: 六、OpenAPI策略分析

    一、如何注册

    • 为什么要注册?

    访问 OpenAPI必须拥有Consumer Key和Consumer Secret。

    • 如何注册?

    要获取Consumer Key及Consumer Secret,需要消费方(Consumer)向服务提供方申请注册,服务提供方审核通过后会向消费方分配Consumer Key和Consumer Secret

    • 注册时需提供什么信息?

    消费方注册时需要向服务提供方提供以下信息:

    条目 说明
    消费方名称 第三方名称
    消费方电子邮箱 API变化时方便通知
    消费方OAuth请求URL地址

    消费方进行OAuth验证,获取令牌及密钥的请求URL地址(不包含参数部分)

    如:http://youwebsite/request_token

    消费方OAuth回调URL地址

    消费方OAuth验证通过后的回调URL地址(不包含参数部分)

    如:http://youwebsite/request_token_ready

    二、如何签名

    对 OpenAPI 进行REST请求时有如下几种参数:

    API方法 api_method
    不需签名参数 UnSignArguments
    需签名参数 SignArguments
    签名 api_signature

    注意:各参数值需要进行url encode编码(尤其中文时)

    调用API时需要对第3种参数进行签名,签名步骤如下:

    No. 说明
    1 所有需签参数名称必须小写
    2 所有参数值要进行url encode编码(尤其中文时),把编码结果转小写
    3 签名参数按字母升序排序
    4 合成签名字符串,合成时把“签名密钥”放到最前面,然后把已排序的需签名参数清单按照“参数名称+参数值”的方式追加到字符串
    5 对合成的签名字符串进行 UTF8 编码
    6 用MD5对签名字符串进行加密生成32位签名

    第4步合成签名串规则如下:

    签名密钥 参数名称 参数值 参数名称 参数值 ......

     

    关于签名密钥的特殊说明:

    调用API 使用的签名密钥
    OpenAPI.OAuth.RequestToken oauth_consumer_secret
    Others oauth_token_secret

    UTF8编码及MD5加密的C#参考实现代码如下:

    using System.Security;
    using System.Security.Cryptography;
    using System.Text;

    //MD5加密
    public static string GetMd5String(string str)
    {
    // First we need to convert the string into bytes, which
    // means using a text encoder.
    byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(str);

    // Now that we have a byte array we can ask the CSP to hash it
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] result = md5.ComputeHash(unicodeText);

    // Build the final string by converting each byte
    // into hex and appending it to a StringBuilder
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < result.Length; i++)
    {
    sb.Append(result[i].ToString("x2"));
    }

    // And return it
    return sb.ToString();
    }

    三、请求REST格式

    请求REST格式由以下几部分组成:

    • 请求REST地址,如:

    http://youserverwebsite/api/rest

    • 调用API方法,如:

    ?api_method=OpenAPI.OAuth.RequestToken

    • 不需签名参数,如:

    &oauth_consumer_key=*************

    • 需签名参数,如:

    &oauth_timestamp=****&oauth_nonce=****&oauth_version=****

    • 签名,如:

    &api_signature=****

    完整请求REST串:

    http://youserverwebsite/api/rest?

    api_method=OpenAPI.oauth.requesttoken

    &oauth_consumer_key=****

    &oauth_nonce=a65c5e5e903942a994bcb07250431e2b

    &oauth_timestamp=1277216551

    &oauth_version=1.0

    &api_signature=6431e9d5b8508a2177920e8c3c624b1b

    四、请求REST返回格式 

    请求REST返回格式为xml格式或者json格式,目前仅支持xml格式,示例如下:

    <rsp method="openapi.oauth.authorizetoken" flag="True" code="" desc="ok">
      <data>    <token>4941c6de38650e58e75a1536d847c2b6</token>
        <token_secret>c48bcf5ba5166a5b5c409e233811025b</token_secret>  </data></rsp>

    返回结果格式说明:

    参数名称 备注说明
    method 请求的API方法名称
    flag 请求成功与否标识:true=成功;false=失败;
    code 结果状态码
    desc 结果说明
    data 结果数据

     

    [返回导航]

  • 相关阅读:
    Caused by: java.net.ConnectException: Connection timed out: connect
    检测是否安装了vsftpd
    如何配置nginx
    如何将文件压缩成.tar.gz格式的文件
    如何在linux中解压.rar文件
    在linux环境中配置solr
    linux环境下查看tomcat日志
    linux环境下安装solr
    在linux环境中配置tomcat
    在linux环境中如何删除文件
  • 原文地址:https://www.cnblogs.com/liuxiaojun/p/openapi_plot.html
Copyright © 2011-2022 走看看