获取用户授权码

如图所示:

  • 用户在前端打开编辑器时,需要通过后端调用接口申请访问的授权码(code)。
  • 合作方拿到授权码(code)后,需要把授权码(code)传入编辑器SDK。
  • 编辑器SDK根据授权码(code)获取用户身份信息,并进行相应的操作。

获取授权码(code)接口

接口说明:根据企业用户ID,获取SDK的授权码,接口调用方式请参考:开放API对接文档

字段

类型

是否必填

字段描述

app_id

string

应用ID

uid

string

对接方用户体系的用户唯一标识,最长50位字符串

  • 响应参数

字段

类型

字段描述

code

string

授权码code

  • 响应示例
{
  "code":"sadfsdfs"
} 


请求示例

Java示例:

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;

public class AuthCodeTest {
    public static String getHMAC(String data, String key) throws Exception {
        String HMAC_SHA1_ALGORITHM = "HmacSHA1";
        SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signinKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        return new String(Base64.encodeBase64(rawHmac));
    }

    public static void main(String[] args) throws Exception {
        // TODO 使用真实AK
        String ak = "********************************";
        // TODO 使用真实SK
        String sk = "********************************";
        String httpMethod = "GET";
        String uri = "/api/authorized/code/";
        // TODO 使用真实接口入参        
        // app_id和uid自行填写!!!
        String queryString = "app_id=***&uid=***";
        long timestamp = System.currentTimeMillis() / 1000;
        String requestRaw = StringUtils.join(new String[]{httpMethod, "@", uri, "@", queryString, "@", Long.toString(timestamp)}, "");
        String signature = getHMAC(requestRaw, sk);
        CloseableHttpClient client = HttpClientBuilder.create().build();
        // app_id和uid自行填写!!!
        HttpGet HttpGet = new HttpGet("https://open-api.gaoding.com/api/authorized/code?app_id=***&uid=***");
        CloseableHttpResponse response = null;
        try {
            HttpGet.addHeader("X-Timestamp", Long.toString(timestamp));
            HttpGet.addHeader("X-AccessKey", ak);
            HttpGet.addHeader("X-Signature", signature);
            response = client.execute(HttpGet);
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } finally {
            try {
                // 释放资源
                if (client != null) {
                    client.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}