码迷,mamicode.com
首页 > 其他好文 > 详细

第三方登录之Gitee

时间:2020-05-22 21:43:21      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:hashmap   avatar   官方文档   commons   set   ota   map   ref   secret   

1,注册应用,获取Client ID

登录码云,鼠标移到头像,点击设置按钮,右边点击第三方应用,点击创建应用

技术图片

填写信息,回调地址一定要填好

技术图片

点击确定获取ClientID,和ClientSecret

技术图片

发请求拉起授权页面

<a href="https://gitee.com/oauth/authorize?client_id= 你的clientId&redirect_uri你的回调地址&response_type=code">gitee登录</a>
 

输入账号密码后,会回调到你自己填的回调地址并且带给你一个参数code,获取code再次发送post请求

https://gitee.com/oauth/token?grant_type=authorization_code&code=你拿到的code&client_id=你自己的clientId&redirect_uri=自己的回调地址&client_secret=你自己的client_secret

然后码云那边会返回给你一个令牌,access_token

拿着这个access_token再次发送get请求去获取用户信息

https://gitee.com/api/v5/user?access_token=拿到的access_token

这里有2个坑:在发送post去获取令牌的时候 必须要把User-Agent设置为: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 

发送get请求获取用户资源信息也是一样。

具体参见:https://gitee.com/oschina/git-osc/issues/IDBSA

依赖

<!-- 下面这个不加好像也可以-->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5</version>
</dependency>
 
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.1</version>
</dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

控制层:

package alu.controller;


import com.alibaba.fastjson.JSONObject;

import alu.util.GitHubConstant;
import alu.util.HttpClientUtils;
import alu.util.HttpUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/gitee")
public class TestController {

@RequestMapping("/callback")
public String callback(String code,Model model) throws Exception{
System.out.println("得到的code为:" + code);
Map<String, String> params = new HashMap<>(5);

String url = "https://gitee.com/oauth/token";
//申请应用时分配的AppKey
params.put("client_id", "00000000000.。。。");
//申请应用时分配的AppSecret
params.put("client_secret", "0.。。。。。。。。。。。。。。");
//请求的类型,填写authorization_code
params.put("grant_type", "authorization_code");
//调用authorize获得的code值
params.put("code", code);
//回调地址,需需与注册应用里的回调地址一致。
params.put("redirect_uri", "http://127.0.0.1:8080/gitee/callback");
try {
String result = HttpUtil.post(url, params);
System.out.println("得到的结果为:" + result);

JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
url = "https://gitee.com/api/v5/user";
String getUserInfo = HttpUtil.get(url, jsonObject.get("access_token"));
System.out.println("得到的用户信息为:" + getUserInfo);
jsonObject = (JSONObject) JSONObject.parse(getUserInfo);
model.addAttribute("userName", jsonObject.get("name"));
model.addAttribute("userImage", jsonObject.get("avatar_url"));
} catch (IOException e) {
e.printStackTrace();
}
// 否则返回到登陆页面
return "/user/success";
}



@RequestMapping("/login")
public String login() throws Exception{

return "/user/login";// TODO 修改成自己需要返回的页面...
}
}

  

HttpUtil

package alu.util;


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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class HttpUtil {

    /**
     * 发送POST请求
     * @param url 请求的接口路径
     * @param params 参数
     * @return 
     * @throws IOException
     */
    public static String post(String url, Map<String, String> params) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuilder stringBuilder = new StringBuilder(url);
      
 
        stringBuilder.append("?grant_type=");
        stringBuilder.append(params.get("grant_type"));
      

        stringBuilder.append("&code=");
        stringBuilder.append(params.get("code"));
        
        stringBuilder.append("&client_id=");
        stringBuilder.append(params.get("client_id"));
        
        stringBuilder.append("&redirect_uri=");
        stringBuilder.append(params.get("redirect_uri"));
        
        stringBuilder.append("&client_secret=");
        stringBuilder.append(params.get("client_secret"));
 

      
        System.out.println("stringBuilder.toString():"+stringBuilder.toString());
        HttpPost httpPost = new HttpPost(stringBuilder.toString());
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36)");
        //发送请求返回响应的信息
        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            return result;
        }
        return null;
    }
    
    public static String get(String url, Object access_token) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuilder stringBuilder = new StringBuilder(url);
        //第一个参数
        stringBuilder.append("?access_token=");
        stringBuilder.append(access_token);
        System.out.println("stringBuilder.toString():"+stringBuilder.toString());
        HttpGet httpGet = new HttpGet(stringBuilder.toString());
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36)");
        //发送请求返回响应的信息
        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            return result;
        }
        return null;
    }


}

  

页面:

技术图片

官方文档:https://gitee.com/api/v5/oauth_doc#/

第三方登录之Gitee

标签:hashmap   avatar   官方文档   commons   set   ota   map   ref   secret   

原文地址:https://www.cnblogs.com/mangoubiubiu/p/12939610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!