Http请求第三方的AccessToken

package com.qw.web_common.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author qw
 * @Description
 * @date 9/5/2022
 */
@Slf4j
@Component
public class Http {
    public String post(String url , String json) throws Exception {
//        BufferedOutputStream  out = null ;
        BufferedReader in = null ;
        StringBuilder sb = new StringBuilder() ;
        try {
            //得到连接对象
            HttpURLConnection connection = getConnection(url);
            //5. 向服务器写入数据
//            out = new BufferedOutputStream(connection.getOutputStream());
//            out.write(json.getBytes());
            //6. 接收服务端的响应
            int responseCode = connection.getResponseCode();
            //7. 响应码为200则读数据
            if (responseCode == 200) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                sb = new StringBuilder();
                String temp = null;
                while (null != (temp = in.readLine())) {
                    sb.append(temp);
                }
            }
        }catch(Exception e){
            throw new Exception(e.getMessage());
        }finally{
//            out.close();
            in.close();
        }
        return sb.toString();
    }

        /**
         *
         *  通过地址获得连接对象
         *
         */
    private synchronized HttpURLConnection getConnection(String url) throws Exception {
        HttpURLConnection connection = null ;
        try {
            //需要将认证的参数放在地址后面拼接(否则有可能报401)
            url=url+"?grant_type=XXX&client_id=XXX&client_secret=XXX&scope=XXX";
//            1. 创建服务地址
            URL realURL = new URL(url);
            connection = (HttpURLConnection) realURL.openConnection();
            //2. 设置发送方式:POST必须大写
            connection.setRequestMethod("POST");
            //3. 设置数据格式:Content-type
            connection.setRequestProperty("Content-type", "application/json");
            //4. 设置输入,新创建的connection默认是没有写权限的,doOutput值默认为false
            connection.setDoOutput(true);
            return connection;
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new Exception("获取发送地址连接出错");
        }
    }
}

postman方式获取accessToken认证
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您使用Spring框架时,可以使用RestTemplate类轻松调用第三方API。以下是使用RestTemplate类进行API调用的简单示例: 1. 导入RestTemplate类: ```java import org.springframework.web.client.RestTemplate; ``` 2. 创建RestTemplate对象: ```java RestTemplate restTemplate = new RestTemplate(); ``` 3. 发送GET请求并接收响应: ```java String apiUrl = "https://api.example.com/data"; String response = restTemplate.getForObject(apiUrl, String.class); ``` 4. 发送POST请求并接收响应: ```java String apiUrl = "https://api.example.com/data"; HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + accessToken); MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>(); requestBody.add("param1", "value1"); requestBody.add("param2", "value2"); HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers); String response = restTemplate.postForObject(apiUrl, requestEntity, String.class); ``` 在这个例子中,我们创建了一个RestTemplate对象,然后使用它发送GET和POST请求。GET请求只需要指定API的URL并调用`getForObject()`方法,而POST请求则需要指定API的URL、请求头、请求体,并调用`postForObject()`方法。最后,我们使用字符串类型的`response`变量接收API的响应。 当然,具体的API调用方式还需要根据第三方API的具体要求来确定。此外,我们还需要考虑错误处理、异常处理和安全性等方面的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值