风丝袅 發表於 2020-5-22 20:38:00

第三方登录之Gitee

<p>1,注册应用,获取Client ID</p>
<p>登录码云,鼠标移到头像,点击设置按钮,右边点击第三方应用,点击创建应用</p>
<p><img src="https://img2020.cnblogs.com/blog/2033226/202005/2033226-20200522200835871-1045785364.png" alt=""></p>
<p>填写信息,回调地址一定要填好</p>
<p><img src="https://img2020.cnblogs.com/blog/2033226/202005/2033226-20200522201018618-62214606.png" alt=""></p>
<p>点击确定获取ClientID,和ClientSecret</p>
<p><img src="https://img2020.cnblogs.com/blog/2033226/202005/2033226-20200522201248691-1227687125.png" alt=""></p>
<p>发请求拉起授权页面</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;a href="https://gitee.com/oauth/authorize?client_id= </span>你的clientId<span style="color: rgba(0, 0, 255, 1)">&amp;redirect_uri</span>你的回调地址<span style="color: rgba(0, 0, 255, 1)">&amp;response_type=code"&gt;gitee登录&lt;/a&gt;</span></pre>
<pre><em id="__mceDel"><span style="color: rgba(0, 0, 255, 1)">&nbsp;</span></em></pre>
</div>
<p>输入账号密码后,会回调到你自己填的回调地址并且带给你一个参数code,获取code再次发送post请求</p>
<p>https://gitee.com/oauth/token?grant_type=authorization_code&amp;code=你拿到的code&amp;client_id=你自己的clientId&amp;redirect_uri=自己的回调地址&amp;client_secret=你自己的client_secret</p>
<p>然后码云那边会返回给你一个令牌,access_token</p>
<p>拿着这个access_token再次发送get请求去获取用户信息</p>
<p>https://gitee.com/api/v5/user?access_token=拿到的access_token</p>
<p><span style="background-color: rgba(255, 255, 255, 1); color: rgba(255, 0, 0, 1)">这里有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&nbsp;</span></p>
<p><span style="color: rgba(255, 0, 0, 1)">发送get请求获取用户资源信息也是一样。</span></p>
<p><span style="color: rgba(255, 0, 0, 1)">具体参见:https://gitee.com/oschina/git-osc/issues/IDBSA</span></p>
<p>依赖</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 下面这个不加好像也可以</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.httpcomponents<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>httpmime<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>4.5<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.httpcomponents<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>httpclient<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>4.5<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.httpcomponents<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>httpcore<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>4.4.1<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.alibaba<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>fastjson<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.2.47<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>控制层:</p>
<div class="cnblogs_code">
<div class="cnblogs_Highlighter">
<pre class="brush:sql;gutter:true;">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&lt;String, String&gt; params = new HashMap&lt;&gt;(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 修改成自己需要返回的页面...
}
}
</pre>
</div>
<p>  </p>
</div>
<p>HttpUtil</p>
<div class="cnblogs_code">
<div class="cnblogs_Highlighter">
<pre class="brush:sql;gutter:true;">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&lt;String, String&gt; params) throws IOException {
      CloseableHttpClient client = HttpClients.createDefault();
      StringBuilder stringBuilder = new StringBuilder(url);
      

      stringBuilder.append("?grant_type=");
      stringBuilder.append(params.get("grant_type"));
      

      stringBuilder.append("&amp;code=");
      stringBuilder.append(params.get("code"));
      
      stringBuilder.append("&amp;client_id=");
      stringBuilder.append(params.get("client_id"));
      
      stringBuilder.append("&amp;redirect_uri=");
      stringBuilder.append(params.get("redirect_uri"));
      
      stringBuilder.append("&amp;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;
    }


}
</pre>
</div>
<p>  </p>
</div>
<p>页面:</p>
<p><img src="https://img2020.cnblogs.com/blog/2033226/202005/2033226-20200522203648340-39446021.png" alt=""></p>
<p>官方文档:https://gitee.com/api/v5/oauth_doc#/</p><br><br>
来源:https://www.cnblogs.com/mangoubiubiu/p/12939610.html
頁: [1]
查看完整版本: 第三方登录之Gitee