internal static async Task <Office365ServiceInfo> CreateAsync()
            {
                // Attempt to build an Office365ServiceInfo object based on cached API endpoint & resource ID information:
                SharePointOneDriveServiceInfo info = new SharePointOneDriveServiceInfo
                {
                    ResourceId  = (string)Office365CommonController.GetFromCache("SharePointOneDriveResourceId"),
                    ApiEndpoint = (string)Office365CommonController.GetFromCache("SharePointOneDriveApiEndpoint")
                };

                // If the Resource ID and API Endpoint are not empty, then the cached information is sufficient:
                if (info.ResourceId != null && info.ApiEndpoint != null)
                {
                    info.AccessToken = Office365CommonController.GetAccessToken(info.ResourceId);
                    return(info);
                }

                // If did not return above, invoke the Discovery Service to obtain the resource ID and API endpoint:
                info._discoveryServiceInfo = new DiscoveryServiceInfo();

                // If no auth header is available for Discovery, return the info as is, with the missing
                //     access token (and possibly a missing ResourceId and ApiEndpoint as well). The caller will need
                //     to do an OAuth redirect anyway.
                if (!info._discoveryServiceInfo.HasValidAccessToken)
                {
                    return(info);
                }

                // If still here, discovery has enough information to obtain the SharePoint OneDrive endpoints:
                DiscoveryResult[] results = await info._discoveryServiceInfo.DiscoverServicesAsync();

                DiscoveryResult myFilesEndpoint = results.First(result => result.Capability == "MyFiles");

                // Update and cache the resource ID and API endpoint:
                info.ResourceId = myFilesEndpoint.ServiceResourceId;
                // NOTE: In the initial Preview release of Service Discovery, the "MyFiles" endpoint URL will always
                //     start with something like "https://contoso-my.sharepoint.com/personal/<username>_contoso_com/_api",
                //     but the path following "/_api" may change over time.  For consistency, it is safer to manually
                //     extract the root path, and then append a call for the location of the Documents folder:
                info.ApiEndpoint = myFilesEndpoint.ServiceEndpointUri.Substring(
                    0, myFilesEndpoint.ServiceEndpointUri.IndexOf("/_api", StringComparison.Ordinal)) +
                                   "/_api/web/getfolderbyserverrelativeurl('Documents')";
                Office365CommonController.SaveInCache("SharePointOneDriveResourceId", info.ResourceId);
                Office365CommonController.SaveInCache("SharePointOneDriveApiEndpoint", info.ApiEndpoint);
                info.AccessToken = Office365CommonController.GetAccessToken(info.ResourceId);
                return(info);
            }
예제 #2
0
            internal async static Task <Office365ServiceInfo> CreateAsync()
            {
                // Attempt to build an Office365ServiceInfo object based on cached API endpoint & resource ID information:
                Office365ServiceInfo info = new SharePointOneDriveServiceInfo()
                {
                    ResourceId  = (string)Office365Helper.GetFromCache("SharePointOneDriveResourceId"),
                    ApiEndpoint = (string)Office365Helper.GetFromCache("SharePointOneDriveApiEndpoint")
                };

                // If the cached Resource ID and API Endpoint are not empty, then use them:
                if ((info.ResourceId != null) && (info.ApiEndpoint != null))
                {
                    info.AccessToken = await Office365Helper.GetAccessToken(info.ResourceId);

                    return(info);
                }

                // If did not return above, invoke the Discovery Service to obtain the resource ID and API endpoint:
                DiscoveryServiceInfo discoveryServiceInfo = await DiscoveryServiceInfo.CreateAsync();

                if (!discoveryServiceInfo.HasValidAccessToken)
                {
                    // Cannot communicated with Service Discovery, so return the empty SharePointOneDriveServiceInfo as is.
                    //     The missing access token will let the caller know that the service is not ready to be used.
                    return(info);
                }

                DiscoveryResult[] results = await discoveryServiceInfo.DiscoverServicesAsync();

                DiscoveryResult myFilesEndpoint = results.First(result => result.Capability == "MyFiles");

                // Update and cache the resource ID and API endpoint:
                info.ResourceId = myFilesEndpoint.ServiceResourceId;
                // NOTE: In the initial Preview release of Service Discovery, the "MyFiles" endpoint URL will always
                //     start with something like "https://contoso-my.sharepoint.com/personal/<username>_contoso_com/_api",
                //     but the path following "/_api" may change over time.  For consistency, it is safer to manually
                //     extract the root path, and then append a call for the location of the Documents folder:
                info.ApiEndpoint = myFilesEndpoint.ServiceEndpointUri.Substring(
                    0, myFilesEndpoint.ServiceEndpointUri.IndexOf("/_api", StringComparison.Ordinal)) +
                                   "/_api/web/getfolderbyserverrelativeurl('Documents')";
                Office365Helper.SaveInCache("SharePointOneDriveResourceId", info.ResourceId);
                Office365Helper.SaveInCache("SharePointOneDriveApiEndpoint", info.ApiEndpoint);
                info.AccessToken = await Office365Helper.GetAccessToken(info.ResourceId);

                return(info);
            }
예제 #3
0
 /// <summary>
 /// Returns information about the SharePoint service, including its cached access token.
 /// Note that for SharePoint, the resource ID and API endpoint will be different for each tenant,
 ///     so that information must be discovered via a Discovery Service before it can be cached.
 /// On error, this method will display an error message to the user, and return an
 ///     Office365ServiceInfo instance whose HasValidAccessToken property is set to "false".
 /// </summary>
 public static async Task <Office365ServiceInfo> GetSharePointOneDriveServiceInfoAsync()
 {
     return(await SharePointOneDriveServiceInfo.CreateAsync());
 }
            internal static async Task<Office365ServiceInfo> CreateAsync()
            {
                // Attempt to build an Office365ServiceInfo object based on cached API endpoint & resource ID information:
                SharePointOneDriveServiceInfo info = new SharePointOneDriveServiceInfo
                {
                    ResourceId = (string) Office365CommonController.GetFromCache("SharePointOneDriveResourceId"),
                    ApiEndpoint = (string) Office365CommonController.GetFromCache("SharePointOneDriveApiEndpoint")
                };

                // If the Resource ID and API Endpoint are not empty, then the cached information is sufficient:
                if (info.ResourceId != null && info.ApiEndpoint != null)
                {
                    info.AccessToken = Office365CommonController.GetAccessToken(info.ResourceId);
                    return info;
                }

                // If did not return above, invoke the Discovery Service to obtain the resource ID and API endpoint:
                info._discoveryServiceInfo = new DiscoveryServiceInfo();

                // If no auth header is available for Discovery, return the info as is, with the missing 
                //     access token (and possibly a missing ResourceId and ApiEndpoint as well). The caller will need
                //     to do an OAuth redirect anyway.
                if (!info._discoveryServiceInfo.HasValidAccessToken)
                {
                    return info;
                }

                // If still here, discovery has enough information to obtain the SharePoint OneDrive endpoints:
                DiscoveryResult[] results = await info._discoveryServiceInfo.DiscoverServicesAsync();
                DiscoveryResult myFilesEndpoint = results.First(result => result.Capability == "MyFiles");

                // Update and cache the resource ID and API endpoint:
                info.ResourceId = myFilesEndpoint.ServiceResourceId;
                // NOTE: In the initial Preview release of Service Discovery, the "MyFiles" endpoint URL will always
                //     start with something like "https://contoso-my.sharepoint.com/personal/<username>_contoso_com/_api",
                //     but the path following "/_api" may change over time.  For consistency, it is safer to manually
                //     extract the root path, and then append a call for the location of the Documents folder:
                info.ApiEndpoint = myFilesEndpoint.ServiceEndpointUri.Substring(
                    0, myFilesEndpoint.ServiceEndpointUri.IndexOf("/_api", StringComparison.Ordinal)) +
                    "/_api/web/getfolderbyserverrelativeurl('Documents')";
                Office365CommonController.SaveInCache("SharePointOneDriveResourceId", info.ResourceId);
                Office365CommonController.SaveInCache("SharePointOneDriveApiEndpoint", info.ApiEndpoint);
                info.AccessToken = Office365CommonController.GetAccessToken(info.ResourceId);
                return info;
            }