Retrieve a user’s details via a Portal.

Use case
This portal information can be used to provide a customized UI experience for the user. For example, you can show a thumbnail next to their username in the header of an application to indicate that they are currently logged in. Additionally, apps such as Collector and Explorer use this functionality to integrate with Portal.
How to use the sample
When prompted, enter your ArcGIS Online credentials.
How it works
- A
Portalis created with a URL - An
OAuthUserConfigurationis created with the Portal URL, a redirect URI, and Client ID. - The
OAuthUserConfigurationis added to the Toolkit’sOAuthUserConfigurationManager, letting theAuthenticatorknow to operate in OAuth mode. - When the app launches, the portal is loaded, which triggers an authentication challenge.
- An
Authenticatorlistens to the challenge and displays a login screen to allow user credentials to be entered. - If the portal is successfully loaded, the
portalUserproperty is used to populate a series of fields including:fullNameusernameemaildescriptionaccess
- Similarly, the
portalInfoproperty is used to populate:organizationNameorganizationDescriptionthumbnailUrlcanSearchPubliccanSharePublic
Relevant API
- Authenticator
- Authentication::AuthenticationManager
- Authentication::OAuthUserConfiguration
- OAuthUserConfigurationManager
- PortalInfo
- PortalUser
About the data
This sample signs into your ArcGIS online account and displays the user’s profile information.
Tags
account, avatar, bio, cloud and portal, email, login, picture, profile, user, username
Sample Code
// [WriteFile Name=PortalUserInfo, Category=CloudAndPortal]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "PortalUserInfo.h"
// ArcGIS Maps SDK headers#include "Authentication/OAuthUserConfiguration.h"#include "Error.h"#include "MapTypes.h"#include "Portal.h"#include "PortalInfo.h"#include "PortalTypes.h"#include "PortalUser.h"
// Qt headers#include <QUrl>
// Other headers#include "OAuthUserConfigurationManager.h"
using namespace Esri::ArcGISRuntime;using namespace Esri::ArcGISRuntime::Authentication;using namespace Esri::ArcGISRuntime::Toolkit;
const QString PortalUserInfo::UNKNOWN = "Unknown";
PortalUserInfo::PortalUserInfo(QQuickItem* parent /* = nullptr */): QQuickItem(parent), m_portal(new Portal(true, this)){ const QString redirectUrl{"urn:ietf:wg:oauth:2.0:oob"}; OAuthUserConfiguration* config = new OAuthUserConfiguration(m_portal->url(), "iLkGIj0nX8A4EJda", redirectUrl, this); OAuthUserConfigurationManager::addConfiguration(config);
connect(m_portal, &Portal::loadStatusChanged, this, &PortalUserInfo::onPortalLoadStatusChanged); connect(m_portal, &Portal::doneLoading, this, &PortalUserInfo::loadErrorMessageChanged);}
PortalUserInfo::~PortalUserInfo() = default;void PortalUserInfo::init(){ qmlRegisterType<PortalUserInfo>("Esri.Samples", 1, 0, "PortalUserInfoSample");}
void PortalUserInfo::componentComplete(){ QQuickItem::componentComplete(); load();}
void PortalUserInfo::load(){ if (!m_portal) return;
if (m_portal->loadStatus() == LoadStatus::NotLoaded) m_portal->load(); else if (m_portal->loadStatus() == LoadStatus::FailedToLoad) m_portal->retryLoad();}
QString PortalUserInfo::username() const{ if (m_user) return m_user->username();
return UNKNOWN;}
bool PortalUserInfo::loaded(){ if (m_portal) return m_portal->loadStatus() == LoadStatus::Loaded;
return false;}
bool PortalUserInfo::loginDismissed(){ if (m_portal) return m_portal->loadError().errorType() == ErrorType::AuthenticationChallengeCanceled;
return false;}
QString PortalUserInfo::fullName() const{ if (m_user) return m_user->fullName();
return UNKNOWN;}
QString PortalUserInfo::email() const{ if (m_user) return m_user->email();
return UNKNOWN;}
QString PortalUserInfo::bio() const{ if (m_user) return m_user->userDescription();
return UNKNOWN;}
QString PortalUserInfo::access() const{ if (!m_user) return UNKNOWN;
switch (m_user->access()) { return UNKNOWN; case PortalAccess::Organization: return "Organization"; case PortalAccess::Private: return "Only you"; case PortalAccess::Public: return "Everyone"; case PortalAccess::Shared: return "Shared Groups"; default: return UNKNOWN; }}
QUrl PortalUserInfo::thumbnailUrl() const{ if (m_user && !m_user->thumbnailUrl().isEmpty()) return m_user->thumbnailUrl();
return QUrl("qrc:/Samples/CloudAndPortal/PortalUserInfo/placeholder_img.png");}
QString PortalUserInfo::orgTitle() const{ if (m_portal && m_portal->portalInfo()) return m_portal->portalInfo()->organizationName();
return "";}
QString PortalUserInfo::orgDescription() const{ if (m_portal && m_portal->portalInfo()) return m_portal->portalInfo()->organizationDescription();
return "";}
QUrl PortalUserInfo::orgThumbnailUrl() const{ if (m_portal && m_portal->portalInfo()) return m_portal->portalInfo()->thumbnailUrl();
return QUrl();}
QString PortalUserInfo::canSearchPublic() const{ if (m_portal && m_portal->portalInfo()) return m_portal->portalInfo()->isCanSearchPublic() ? "True" : "False";
return "";}
QString PortalUserInfo::canSharePublic() const{ if (m_portal && m_portal->portalInfo()) return m_portal->portalInfo()->isCanSharePublic() ? "True" : "False";
return "";}
QString PortalUserInfo::loadErrorMessage() const{ if (m_portal) return m_portal->loadError().message();
return "";}
void PortalUserInfo::onPortalLoadStatusChanged(LoadStatus loadStatus){ switch (loadStatus) { case LoadStatus::Loaded: if (m_portal) { m_user = m_portal->portalUser(); connect(m_user, &PortalUser::thumbnailUrlChanged, this, &PortalUserInfo::thumbnailUrlChanged); } emit fullNameChanged(); emit usernameChanged(); emit emailChanged(); emit bioChanged(); emit accessChanged(); emit thumbnailUrlChanged(); break; case LoadStatus::Loading: break; case LoadStatus::FailedToLoad: break; case LoadStatus::NotLoaded: break; case LoadStatus::Unknown: break; default: break; }
emit loadedChanged(); emit isLoginDismissed();}// [WriteFile Name=PortalUserInfo, Category=CloudAndPortal]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifndef PORTALUSERINFO_H#define PORTALUSERINFO_H
// Qt headers#include <QQuickItem>#include <QUrl>
namespace Esri::ArcGISRuntime{ class Portal; class PortalUser; enum class LoadStatus;}
class PortalUserInfo : public QQuickItem{ Q_OBJECT
Q_PROPERTY(QString username READ username NOTIFY usernameChanged) Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) Q_PROPERTY(QString fullName READ fullName NOTIFY fullNameChanged) Q_PROPERTY(QString email READ email NOTIFY emailChanged) Q_PROPERTY(QString bio READ bio NOTIFY bioChanged) Q_PROPERTY(QString access READ access NOTIFY accessChanged) Q_PROPERTY(QUrl thumbnailUrl READ thumbnailUrl NOTIFY thumbnailUrlChanged) Q_PROPERTY(QString orgTitle READ orgTitle NOTIFY loadedChanged) Q_PROPERTY(QString orgDescription READ orgDescription NOTIFY loadedChanged) Q_PROPERTY(QUrl orgThumbnailUrl READ orgThumbnailUrl NOTIFY loadedChanged) Q_PROPERTY(QUrl canSearchPublic READ canSearchPublic NOTIFY loadedChanged) Q_PROPERTY(QUrl canSharePublic READ canSharePublic NOTIFY loadedChanged) Q_PROPERTY(QString loadErrorMessage READ loadErrorMessage NOTIFY loadedChanged) Q_PROPERTY(bool loginDismissed READ loginDismissed NOTIFY isLoginDismissed)
public: explicit PortalUserInfo(QQuickItem* parent = nullptr); ~PortalUserInfo() override;
void componentComplete() override; static void init(); Q_INVOKABLE void load();
signals: void loadedChanged(); void usernameChanged(); void fullNameChanged(); void emailChanged(); void bioChanged(); void accessChanged(); void thumbnailUrlChanged(); void loadErrorMessageChanged(); void isLoginDismissed();
private slots: void onPortalLoadStatusChanged(Esri::ArcGISRuntime::LoadStatus loadStatus);
private: QString username() const; bool loaded(); QString fullName() const; QString email() const; QString bio() const; QString access() const; QUrl thumbnailUrl() const; QString orgTitle() const; QString orgDescription() const; QUrl orgThumbnailUrl() const; QString canSearchPublic() const; QString canSharePublic() const; QString loadErrorMessage() const; bool loginDismissed();
static const QString UNKNOWN;
Esri::ArcGISRuntime::Portal* m_portal = nullptr; Esri::ArcGISRuntime::PortalUser* m_user = nullptr;};
#endif // PORTALUSERINFO_H// [WriteFile Name=PortalUserInfo, Category=CloudAndPortal]// [Legal]// Copyright 2016 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samplesimport Esri.ArcGISRuntime.Toolkit
PortalUserInfoSample { id: rootRectangle clip: true width: 800 height: 600
readonly property var detailNames: ["Full name", "Username", "Email", "Bio", "Who can see your profile?"] readonly property var detailValue: ["fullName", "username", "email", "bio", "access"] readonly property var infoLabels: ["Description", "Can Find External Content", "Can Share Items Externally"] readonly property var infoValues: ["orgDescription", "canSearchPublic", "canSharePublic"]
BusyIndicator { id: loadingIndicator anchors.centerIn: parent running: !loaded && !loginDismissed }
Column { id: userDetailsColumn visible: loaded anchors { top: parent.top left: parent.left right: parent.right margins: 10 } spacing: 10
Text { text: fullName.length > 0 ? fullName + " Profile" : ("????") font.bold: true font.pointSize: 15 }
Image { source : thumbnailUrl.toString().length > 0 ? thumbnailUrl : "qrc:/Samples/CloudAndPortal/PortalUserInfo/placeholder_img.png" height: 32 width: 32 } }
ListView { visible: loaded anchors { top: userDetailsColumn.bottom bottom: midLine.top left: parent.left right: parent.right margins: 10 } spacing: 10 clip: true model: detailNames.length
delegate: Column { Text { text: detailNames[index] font.bold: true }
Text { text: rootRectangle[detailValue[index]] color: "grey" } } }
Rectangle { id: midLine anchors { verticalCenter: parent.verticalCenter margins: 8 left: parent.left right: parent.right } height: 4 visible: loaded color: "lightgrey" }
Column { id: portalDetailsColumn visible: loaded anchors { top: midLine.bottom left: parent.left right: parent.right margins: 10 } spacing: 10
Text { text: orgTitle font.bold: true font.pointSize: 15 }
Image { source : orgThumbnailUrl height: 32 width: 32 } }
ListView { id: infoList visible: loaded anchors { top: portalDetailsColumn.bottom bottom: parent.bottom left: parent.left right: parent.right margins: 10 } spacing: 10 clip: true model: infoValues.length
delegate: Column { Text { text: loaded ? infoLabels[index] : "" font.bold: true }
Text { text: rootRectangle[infoValues[index]] color: "grey" } } }
Button { id: authenticationPortal anchors { margins: 32 horizontalCenter: parent.horizontalCenter top: parent.top } text: "Authenticate Portal" icon.source: "qrc:/Samples/CloudAndPortal/PortalUserInfo/ic_menu_account_dark.png"
visible: loginDismissed onClicked: { load(); } }
// Declare Authenticator to handle any authentication challenges Authenticator { id: authView anchors.fill: parent }}// [Legal]// Copyright 2015 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>
#ifdef QT_WEBVIEW_WEBENGINE_BACKEND#include <QtWebEngineQuick>#endif // QT_WEBVIEW_WEBENGINE_BACKEND
#ifdef Q_OS_WIN#include <Windows.h>#endif
#include "Esri/ArcGISRuntime/Toolkit/register.h"#include "ArcGISRuntimeEnvironment.h"
#include "PortalUserInfo.h"
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false);
#ifdef QT_WEBVIEW_WEBENGINE_BACKEND QtWebEngineQuick::initialize();#endif // QT_WEBVIEW_WEBENGINE_BACKEND
QGuiApplication app(argc, argv); app.setApplicationName(QString("Portal User Info"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample PortalUserInfo::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive); #endif
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
Esri::ArcGISRuntime::Toolkit::registerComponents(*(view.engine()));
// Set the source view.setSource(QUrl("qrc:/Samples/CloudAndPortal/PortalUserInfo/PortalUserInfo.qml"));
view.show();
return app.exec();}