赞
踩
创建 OAuth 消费者:
获取 Access Token:
示例请求:
curl -X POST -u "<client_id>:<client_secret>" https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials
这个请求将返回一个 JSON 对象,其中包含 access_token
。
创建 App Password:
使用 App Password 进行认证:
示例请求:
curl -u "<username>:<app_password>" https://api.bitbucket.org/2.0/repositories/<username>
生成个人访问令牌(Personal Access Token):
api
、read_user
等。使用个人访问令牌进行认证:
示例请求:
curl --header "PRIVATE-TOKEN: <access_token>" https://gitlab.example.com/api/v4/projects
认证方式:
用途和适用场景:
权限控制:
api
、read_user
、read_repository
等,提供了详细的权限控制。curl -X POST -u "<client_id>:<client_secret>" https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials
import requests
from requests.auth import HTTPBasicAuth
client_id = '<your_client_id>'
client_secret = '<your_client_secret>'
auth_url = 'https://bitbucket.org/site/oauth2/access_token'
auth_response = requests.post(auth_url, data={'grant_type': 'client_credentials'}, auth=HTTPBasicAuth(client_id, client_secret))
auth_data = auth_response.json()
access_token = auth_data['access_token']
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get('https://api.bitbucket.org/2.0/repositories/<username>', headers=headers)
print(response.json())
curl --header "PRIVATE-TOKEN: <access_token>" https://gitlab.example.com/api/v4/projects
import requests
access_token = '<your_access_token>'
headers = {'PRIVATE-TOKEN': access_token}
response = requests.get('https://gitlab.example.com/api/v4/projects', headers=headers)
print(response.json())
通过以上解释和示例,您可以根据需要选择合适的方法和工具进行 Bitbucket 和 GitLab 的 API 访问和集成。
上述提到的两种方式(OAuth 2.0 和 App Passwords)都可行,具体选择哪种方式取决于你的应用场景和需求。
以下是使用 OAuth 2.0 获取 Access Token 并调用 API 的详细示例:
import org.apache.http.client.methods.CloseableHttpResponse; 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.HttpClients; import org.apache.http.util.EntityUtils; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; public class BitbucketOAuth { private static final String CLIENT_ID = "<your_client_id>"; private static final String CLIENT_SECRET = "<your_client_secret>"; private static final String TOKEN_URL = "https://bitbucket.org/site/oauth2/access_token"; public static String getAccessToken() throws Exception { CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(TOKEN_URL); String auth = CLIENT_ID + ":" + CLIENT_SECRET; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes()); post.setHeader("Authorization", "Basic " + encodedAuth); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setEntity(new StringEntity("grant_type=client_credentials")); CloseableHttpResponse response = client.execute(post); String responseBody = EntityUtils.toString(response.getEntity()); client.close(); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map = mapper.readValue(responseBody, Map.class); return (String) map.get("access_token"); } public static void main(String[] args) throws Exception { String accessToken = getAccessToken(); System.out.println("Access Token: " + accessToken); } }
以下是使用 App Passwords 进行 API 调用的详细示例:
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; public class BitbucketAppPassword { private static final String BASE_URL = "https://api.bitbucket.org/2.0/"; private static final String USERNAME = "<your_username>"; private static final String APP_PASSWORD = "<your_app_password>"; public static String getBranches(String username, String repoSlug) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); String url = BASE_URL + "repositories/" + username + "/" + repoSlug + "/refs/branches"; HttpGet get = new HttpGet(url); String auth = USERNAME + ":" + APP_PASSWORD; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes()); get.setHeader("Authorization", "Basic " + encodedAuth); CloseableHttpResponse response = client.execute(get); String responseBody = EntityUtils.toString(response.getEntity()); client.close(); return responseBody; } public static void main(String[] args) throws Exception { String branches = getBranches(USERNAME, "<repo_slug>"); System.out.println("Branches: " + branches); } }
两种方式各有优缺点,可以根据实际需求选择合适的认证方式。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。