We must use here Powershell Management tool for sharepoint online.

I posted here how to use it:Connecting to Sharepoint online site using SharePoint Online Management Shell

Manage external sharing for Office 365 Group site collections

By default, all SharePoint site collections that are part of an Office 365 Group have the sharing setting set to Allow external users who accept sharing invitations and sign in as authenticated users. To change this setting, you can use the Set-SPOSiteWindows PowerShell cmdlet to update the SharingCapability parameter.

Example:

Set-SPOSite -Identity https://contoso.sharepoint.com/sites/site1 -SharingCapability SharingOption

For SharingOption, use one of the following parameters:

Parameter Description
Disabled Don’t allow sharing outside your organization.
ExistingExternalUserSharingOnly Allow sharing only with the external users that already exist in your organization’s directory.
ExternalUserSharingOnly Allow external users who accept sharing invitations and sign in as authenticated users.
ExternalUserAndGuestSharing Allow sharing with all external users, and by using anonymous access links.

So here we will use ExternalUserAndGuestSharing

Then powershelgl code will become:

Set-SPOSite -Identity https://contoso.sharepoint.com/sites/site1 -SharingCapability ExternalUserAndGuestSharing

Reference: https://support.office.com/en-us/article/turn-external-sharing-on-or-off-for-sharepoint-online-6288296a-b6b7-4ea4-b4ed-c297bf833e30#ID0EAABAAA=Office_365_Groups