jenkins 正在尝试连接到onedrives API

dl5txlt9  于 11个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(137)

我正试图将我目前的一些Jenkins作业与单驱动器API集成,以便将Jenkins中的文件发布到共享的单驱动器文件夹中。然而,我在一个驱动器中进行身份验证时遇到了问题,我的bash脚本如下所示:

#!/bin/bash
# Define your variables
CLIENT_ID="<>"
CLIENT_SECRET="<>"
TENANT_ID="<>"
# Define the folder where your Jenkins instance stores the files
SOURCE_FOLDER="/Users/<>/Desktop/TEST"

# Define the folder path in OneDrive where you want to upload the files
ONEDRIVE_FOLDER_PATH=""

# Authenticate and obtain an access token
TOKEN_RESPONSE=$(curl -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/token" \
     -d "client_id=$CLIENT_ID" \
     -d "client_secret=$CLIENT_SECRET" \
     -d "grant_type=client_credentials" \
     -d "scope=https://graph.microsoft.com/.default")

# Extract the access token from the JSON response
ACCESS_TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.access_token')

if [ -z "$ACCESS_TOKEN" ]; then
    echo "Failed to obtain an access token."
    exit 1
fi

# Loop through files in the source folder and upload them to OneDrive
for FILE in "$SOURCE_FOLDER"; do
    FILE_NAME=$(basename "$FILE")
    ONEDRIVE_API_URL="https://graph.microsoft.com/v1.0/me/drive/root:$ONEDRIVE_FOLDER_PATH/$FILE_NAME:/content"
    ONEDRIVE_API_URL="https://$TENANT_ID.sharepoint.com/CRM/_api/v2.0/drive/root:$ONEDRIVE_FOLDER_PATH/$FILE_NAME:/content"

    # Calculate the content length of the file
    CONTENT_LENGTH=$(wc -c < "$FILE")

    # Use curl to upload the file to OneDrive with the "Content-Length" header
    UPLOAD_RESPONSE=$(curl -X PUT -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Length: $CONTENT_LENGTH" --upload-file "$FILE" "$ONEDRIVE_API_URL")

    if [ $? -eq 0 ]; then
        echo "File '$FILE_NAME' uploaded to OneDrive successfully."
    else
        echo "Failed to upload file '$FILE_NAME' to OneDrive."
    fi
done

# Exit with success
exit 0

字符串
我得到的错误如下:
{“code”:“InvalidAuthenticationToken”,“message”:“访问令牌验证失败。访问者无效。",“innerError”:{“date”:“2023-09- 28 T14:25:58”,“request-id”:“<>",“client-request-id”:“<>"}}

nnvyjq4y

nnvyjq4y1#

该错误与onedrive无关。您有一个身份验证错误

创建账号和凭证

使用此URL获取交换clientid/clientsecret的令牌:
https://login.microsoftonline.com/consumers/oauth2/v2.0/token

详细说明

我验证了一个新帐户。这些是从头开始获取令牌的步骤:

1访问https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade/quickStartType~/null/sourceType/Microsoft_AAD_IAM(应用程序>应用程序注册)

x1c 0d1x的数据
如果您使用公司帐户而不是个人帐户,

2找到您想要的客户端ID的应用程序


3在概述中,您将找到客户端ID:


4导航到证书和机密:

5点击New client secret并生成一个新的secret



x1c4d 1x的

获取令牌

按照oauth2规范配置http客户端
https://www.oauth.com/oauth2-servers/access-tokens/client-credentials/

** Postman **


卷发

curl --location 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=1a57b0e1-fe22-415a-9aa8-149309fc17f5' \
--data-urlencode 'client_secret=5@Q9iT0by54NM4B#UQTdx9OHpeG#PReEIgRrnj07' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=https://graph.microsoft.com/.default'

字符串
注意:clientid和clientsecret只是示例。请使用您自己的值
对于企业,在令牌URL中使用tenant-id而不是“consumer”

相关问题