public async Task <JObject> GetUserProfileAsync() { Credentials credentials = await Credentials.FromSessionAsync(); if (credentials == null) { return(null); } // the API SDK UserProfileApi userApi = new UserProfileApi(); userApi.Configuration.AccessToken = credentials.TokenInternal; // get the user profile dynamic userProfile = await userApi.GetUserProfileAsync(); // prepare a response with name & picture dynamic response = new JObject(); response.name = string.Format("{0} {1}", userProfile.firstName, userProfile.lastName); response.picture = userProfile.profileImages.sizeX40; return(response); }
public async Task <UserResponse> GetUserProfile() { string sessionId, localId; if (!HeaderUtils.GetSessionLocalIDs(out sessionId, out localId)) { return(null); } if (!await OAuthDB.IsSessionIdValid(sessionId, localId)) { return(null); } string userAccessToken = await OAuthDB.GetAccessToken(sessionId, localId); UserProfileApi userProfileApi = new UserProfileApi(); userProfileApi.Configuration.AccessToken = userAccessToken; try { dynamic user = await userProfileApi.GetUserProfileAsync(); UserResponse response = new UserResponse(); response.firstName = user.firstName; response.lastName = user.lastName; return(response); } catch (Exception e) { return(null); } }
private async Task <bool> IsAccountAdmin(string hubId, Credentials credentials) { UserProfileApi userApi = new UserProfileApi(); userApi.Configuration.AccessToken = credentials.TokenInternal; dynamic profile = await userApi.GetUserProfileAsync(); // 2-legged account:read token TwoLeggedApi oauth = new TwoLeggedApi(); dynamic bearer = await oauth.AuthenticateAsync(Config.GetAppSetting("FORGE_CLIENT_ID"), Config.GetAppSetting("FORGE_CLIENT_SECRET"), "client_credentials", new Scope[] { Scope.AccountRead }); RestClient client = new RestClient(Config.BaseUrl); RestRequest thisUserRequest = new RestRequest("/hq/v1/accounts/{account_id}/users/search?email={email}&limit=1", RestSharp.Method.GET); thisUserRequest.AddParameter("account_id", hubId.Replace("b.", string.Empty), ParameterType.UrlSegment); thisUserRequest.AddParameter("email", profile.emailId, ParameterType.UrlSegment); thisUserRequest.AddHeader("Authorization", "Bearer " + bearer.access_token); IRestResponse thisUserResponse = await client.ExecuteTaskAsync(thisUserRequest); dynamic thisUser = JArray.Parse(thisUserResponse.Content); string role = thisUser[0].role; return(role == "account_admin"); }
/// <summary> /// Get profile for the user with the access token. /// </summary> /// <param name="token">Oxygen access token.</param> /// <returns>Dynamic object with User Profile</returns> /// <remarks> /// User Profile fields: https://forge.autodesk.com/en/docs/oauth/v2/reference/http/users-@me-GET/#body-structure-200 /// </remarks> public async Task <dynamic> GetProfileAsync(string token) { var api = new UserProfileApi(new Configuration { AccessToken = token }); return(await api.GetUserProfileAsync()); }
private static async Task <string> GetUserId(Credentials credentials) { UserProfileApi userApi = new UserProfileApi(); userApi.Configuration.AccessToken = credentials.TokenInternal; dynamic userProfile = await userApi.GetUserProfileAsync(); return(userProfile.userId); }
/* * Unhandled exception rendering component: System.Net.Requests is not supported on this platform. * System.PlatformNotSupportedException: System.Net.Requests is not supported on this platform. * at System.Net.WebRequest.get_DefaultWebProxy() * at RestSharp.RestClient.ConfigureHttp(IRestRequest request) * at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback, String httpMethod, Func`4 getWebRequest) * at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback, Method httpMethod) * at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback) * at RestSharp.RestClient.ExecuteAsync(IRestRequest request, CancellationToken token) * --- End of stack trace from previous location --- * at Autodesk.Forge.Client.ApiClient.CallApiAsync(String path, Method method, Dictionary`2 queryParams, Object postBody, Dictionary`2 headerParams, Dictionary`2 formParams, Dictionary`2 fileParams, Dictionary`2 pathParams, String contentType) * at Autodesk.Forge.UserProfileApi.GetUserProfileAsyncWithHttpInfo() * at Autodesk.Forge.UserProfileApi.GetUserProfileAsync() * at BlazorImplicitGrantForge.Pages.Index.GetNameFromProfileAsync() in d:\Demos\BlazorImplicitGrantForge\BlazorImplicitGrantForge\Pages\Index.razor.cs:line 42 * at BlazorImplicitGrantForge.Pages.Index.OnInitializedAsync() in d:\Demos\BlazorImplicitGrantForge\BlazorImplicitGrantForge\Pages\Index.razor.cs:line 32 * at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() */ private async Task <string> GetNameFromProfileAsync() { UserProfileApi userApi = new UserProfileApi(); userApi.Configuration.AccessToken = TokenService.AccessToken; dynamic userProfile = await userApi.GetUserProfileAsync().ConfigureAwait(false); return(string.Format("{0} {1}", userProfile.firstName, userProfile.lastName)); }
private async Task <bool> IsAccountAdmin(string hubId, string accountReadToken, Credentials credentials) { UserProfileApi userApi = new UserProfileApi(); userApi.Configuration.AccessToken = credentials.TokenInternal; dynamic profile = await userApi.GetUserProfileAsync(); RestClient client = new RestClient(BASE_URL); RestRequest thisUserRequest = new RestRequest("/hq/v1/accounts/{account_id}/users/search?email={email}&limit=1", RestSharp.Method.GET); thisUserRequest.AddParameter("account_id", hubId.Replace("b.", string.Empty), ParameterType.UrlSegment); thisUserRequest.AddParameter("email", profile.emailId, ParameterType.UrlSegment); thisUserRequest.AddHeader("Authorization", "Bearer " + accountReadToken); IRestResponse thisUserResponse = await client.ExecuteTaskAsync(thisUserRequest); dynamic thisUser = JArray.Parse(thisUserResponse.Content); string role = thisUser[0].role; return(role == "account_admin"); }