Sharepoint api /sharelink folder

BiYO 1 Reputation point
2023-01-17T14:47:33.65+00:00

Hi All,

I try to create an shared link for a folder but my data parametrs doesn't work and i don't know why ...

For information i need to share this folder in edit and in my organization. (my data variable it is a test)

I share you my part of code :

url = "https://group.sharepoint.com/sites/test/_api/web/GetFolderByServerRelativeUrl('/sites/test/Shared Documents/test_api/myfolder')/ListItemAllFields/ShareLink"
headers = {
    'Accept': 'application/json;odata=verbose',
    'APIKEY': 'fFgfzefezfefefq5wnb1I3CMK7Rnx',
    'Authorization': 'Bearer ' + token + '',
}
data = {'request':
        {'createLink': 'true',
            'settings':
                {'allowAnonymousAccess': 'true', 'role': '8', 'applicationLink': 'false', 'limitUseToApplication': 'false'}}
        }

request = requests.request("POST", url=url, headers=headers, data=data)
print(request.json())

I know they are the Office-365-python library but i can't use them sadly.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-01-18T07:10:16.7666667+00:00

    Hi @BiYO

    Have you register app only correctly? You need to grant access to app only by following steps

    [https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

    Then you need to post request to

    https://accounts.accesscontrol.windows.net/tenant_id/tokens/OAuth/2

    With following data

    client_id = 'xxxx-xxxxx-xxxxxx-xxxx'
    client_secret = 'xxxxxxxxxxxxxxxxxx'
    tenant =  'tenant' # e.g. https://tenant.sharepoint.com
    tenant_id = 'xxxx-xxx-xxxxx-xxx-xxxxx'  
    client_id = client_id + '@' + tenant_id
    
    data = {
        'grant_type':'client_credentials',
        'resource': "00000003-0000-0ff1-ce00-000000000000/" + tenant + ".sharepoint.com@" + tenant_id, 
        'client_id': client_id,
        'client_secret': client_secret,
    }
    
    headers = {
        'Content-Type':'application/x-www-form-urlencoded'
    }
    
    url = "https://accounts.accesscontrol.windows.net/tenant_id/tokens/OAuth/2"
    r = requests.post(url, data=data, headers=headers)
    json_data = json.loads(r.text)
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.