第三方登錄之Gitee


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#/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM