Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

Open JavaScript Onedrive file picker with access token fetched from other application

What I wish to accomplish is to get an access token in one application and use this access token inside another to open a Onedrive file picker. The result should be that the second application does not require to enter credentials.

The login code for application 1:

function handleLogin() {
    const loginWindow = window.open(`https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${clientId}&scope=${scope}
  &response_type=code&redirect_uri=${redirectUri}`, "msgwindow", "width=400, height=500")

    loginWindow.onbeforeunload = function() {
        console.log(loginWindow)
    }

    const interval = setInterval(() => {
        try{
            const signInCode = new URL(loginWindow.location.href).searchParams.get('code');

            if(signInCode) {
                clearInterval(interval);
                loginWindow.close();

                const formData = [];
                formData.push(`client_id=${clientId}`);
                formData.push(`redirect_uri=${redirectUri}`);
                formData.push(`client_secret=${clientSecret}`);
                formData.push(`code=${signInCode}`);
                formData.push(`grant_type=authorization_code`);

                fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
                    method:"POST",
                    headers: {
                        "Content-type":"application/x-www-form-urlencoded"
                    },
                    body: formData.join("&")
                })
                    .then(res => res.json())
                    .then(res => {
                        const tokenDOM = document.querySelector('#tokens')
                        tokenDOM.innerHTML = `<p>ACCESS</p><p>${res.access_token}</p>`;
                    })
                    .catch(e => console.log(e))
            }
        }
        catch (e) {
        }
    }, 100);
}

This generates the access token.

Application 2:


function openFilePicker(accessToken) {

    const options = {
        clientId: clientId,
        action: "share",
        multiSelect: true,
        advanced: {
            accessToken: accessToken,
            endpointHint: 'api.onedrive.com'
        },
        success: function(files) {
            console.log(files);
        },
        cancel: function() {
            console.log('cancel')
        },
        error: function (e) {
            console.log('error', e);
        }
    }

    OneDrive.open(options);
}

Does anyone have an idea why this is not working?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...