Forum Discussion
AMG365
Apr 02, 2025Copper Contributor
SPO connection via client id/secret
hi all, I am trying to get the SPO site name but when running locally I get this error:
"Invalid RedirectURI was received. Not parseable into System.Uri class."
I thought you can use client id/secret with proper api permission to connect to SPO with no issues... Below the code I use.
Thanks!
------------------------
Imports Microsoft.SharePoint.Client
Imports System.Net
Imports System.Security
Imports PnP.Frameworks
Public Class index
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim siteUrl As String = "https://mysite.sharepoint.com/sites/admin"
Dim clientId As String = "xxxxxxxxx"
Dim clientSecret As String = "yyyyyyyyy"
Dim tenantId As String = "555555555555"
Dim siteName As String = GetSharePointSiteName(siteUrl, clientId, clientSecret, tenantId)
Label1.Text = ("Site Name: " & siteName)
End Sub
Function GetSharePointSiteName(siteUrl As String, clientId As String, clientSecret As String, tenantId As String) As String
Dim authManager As New PnP.Framework.AuthenticationManager(clientId, clientSecret, tenantId)
Using context As ClientContext = authManager.GetContext(siteUrl)
Dim web As Web = context.Web
context.Load(web, Function(w) w.Title)
context.ExecuteQuery()
Return web.Title
End Using
End Function
End Class
- AtticusGroveIron Contributor
1. Preparation
Register the Azure AD application
Access Azure Portal
Go to Azure Active Directory > Application Registration
Click “New Registration”, enter a name, and select “This Organizational Directory only”
Record the Record the Application (Client) ID and Tenant ID
2. Create Client Secret
On the Application Registration page, go to “Certificates and Passwords”
Click on “New Client Secret” and set the expiration date (12 months is recommended)
Copy Key Value (shown only once, need to save it properly)
3. Configure API Privileges
Go to “API Privileges”
Add Sites.FullControl.All (for read/write) or Sites.Read.All (for read only)
Administrator needs to "Grant Consent "
4. Connecting to SharePoint Online
powershell
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -ClientId "xxxx-xxxx-xxxx" -ClientSecret "your-secret"
Scenarios: automation scripts, batch operations
5. Using Microsoft Graph API
http
POST https://login.microsoftonline. com/{tenant-id}/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
client_id={client-id}
&scope=https://graph.microsoft.com/.default
&client_secret={client-secret}
&grant_type=client_credentials
Scenario: REST API call
6. Graph SDK)
csharp
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);