Esempio n. 1
0
            internal static async Task <Office365ServiceInfo> CreateAsync()
            {
                // For Exchange, the resource ID and API Endpoint are static for the public O365 cloud.
                Office365ServiceInfo info = new ExchangeServiceInfo()
                {
                    ResourceId  = "https://outlook.office365.com/",
                    ApiEndpoint = "https://outlook.office365.com/ews/odata"
                };

                info.AccessToken = await Office365Helper.GetAccessToken(info.ResourceId);

                return(info);
            }
Esempio n. 2
0
            public static async Task <ActiveDirectoryServiceInfo> CreateAsync()
            {
                // For Active Directory, the resource ID and API Endpoint are static for the public O365 cloud.
                ActiveDirectoryServiceInfo info = new ActiveDirectoryServiceInfo
                {
                    ResourceId  = "https://graph.windows.net/",
                    ApiEndpoint = "https://graph.windows.net"
                };

                info.AccessToken = await Office365Helper.GetAccessToken(info.ResourceId);

                return(info);
            }
Esempio n. 3
0
            internal static async Task <DiscoveryServiceInfo> CreateAsync()
            {
                DiscoveryServiceInfo info = new DiscoveryServiceInfo()
                {
                    // In the initial Preview release of Service Discovery, you must use a temporary Resource ID
                    //     for Service Discovery ("Microsoft.SharePoint"), which will eventually be replaced with a different value.
                    // TODO: If this Resource ID ceases to work, check for an updated value at http://go.microsoft.com/fwlink/?LinkID=392944
                    ResourceId = "Microsoft.SharePoint",

                    ApiEndpoint = "https://api.office.com/discovery/me"
                };

                info.AccessToken = await Office365Helper.GetAccessToken(info.ResourceId);

                return(info);
            }
Esempio n. 4
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);
            }
Esempio n. 5
0
            /// <summary>
            /// Returns information obtained via Discovery. Will throw an exception on error.
            /// </summary>
            internal async Task <DiscoveryResult[]> DiscoverServicesAsync()
            {
                // Create a URL for retrieving the data:
                string requestUrl = String.Format(CultureInfo.InvariantCulture,
                                                  "{0}/services",
                                                  ApiEndpoint);

                // Prepare the HTTP request:
                using (HttpClient client = new HttpClient())
                {
                    Func <HttpRequestMessage> requestCreator = () =>
                    {
                        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
                        request.Headers.Add("Accept", "application/json;odata=verbose");
                        return(request);
                    };

                    // Send the request using a helper method, which will add an authorization header to the request,
                    // and automatically retry with a new token if the existing one has expired.
                    using (HttpResponseMessage response = await Office365Helper.SendRequestAsync(
                               this, client, requestCreator))
                    {
                        // Read the response and deserialize the data:
                        string responseString = await response.Content.ReadAsStringAsync();

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception("Could not obtain discovery information. Service returned " +
                                                response.StatusCode + ":\n\n" + responseString);
                        }

                        // If successful, return the discovery results
                        return(JObject.Parse(responseString)["d"]["results"].ToObject <DiscoveryResult[]>());
                    }
                }
            }