0

我有一个 Xamarin 应用程序,它主要与本地 WIFI 路由器(没有互联网访问权限)连接以与一些本地硬件设备进行通信。对于某些功能,该应用程序使用 NSUrlSessionMultipathServiceType.Handover(手机的蜂窝网络)连接到基于互联网的 API。所有这一切都使用下面给出的代码完美地工作:

/// <summary>
        /// Downloads file from dropbox
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public Task<string> GetFile(string filepath)
        {
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
            NSUrl url = NSUrl.FromString(AppConfig.DropboxFileDownloadBaseUrl + AppConfig.DropboxFileDownloadEndpointUrl);

            try
            {
                NSUrlRequest req = new NSUrlRequest(url);
                var BearerToken = "Bearer " + AppConfig.DropboxAccessToken;
                var DropboxPathHeader = "{\"path\":\"" + filepath + "\"}";

                if (!string.IsNullOrEmpty(BearerToken))
                {
                    NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(url);

                    try
                    {
                        NSMutableDictionary dictionary = new NSMutableDictionary();
                        dictionary.Add(new NSString("Authorization"), new NSString(BearerToken));
                        dictionary.Add(new NSString("Dropbox-API-Arg"), new NSString(DropboxPathHeader));
                        mutableRequest.Headers = dictionary;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    mutableRequest.HttpMethod = "POST";
                    req = (NSUrlRequest)mutableRequest.Copy();
                }



                NSUrlSession session = null;
                NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
                myConfig.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

                session = NSUrlSession.FromConfiguration(myConfig);
                NSUrlSessionTask task = session.CreateDataTask(req, (data, response, error) =>
                {
                    if (response is NSHttpUrlResponse)
                    {
                        var objNSHttpUrlResponse = response as NSHttpUrlResponse;
                        Console.WriteLine(objNSHttpUrlResponse.StatusCode);

                        if (objNSHttpUrlResponse.StatusCode == 200)
                        {
                            //Console.WriteLine(data);
                            byte[] dataBytes = new byte[data.Length];
                            Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));

                            string enc_json = Encoding.Unicode.GetString(dataBytes);


                            //tell the TaskCompletionSource that we are done here:
                            tcs.TrySetResult(enc_json);
                        }
                        else
                        {
                            //tell the TaskCompletionSource that we are done here:
                            tcs.TrySetResult(null);
                        }
                    }
                    else
                    {
                        //tell the TaskCompletionSource that we are done here:
                        tcs.TrySetResult(null);
                    }


                });

                task.Resume();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                //tell the TaskCompletionSource that we are done here:
                tcs.TrySetResult(null);
            }

            return tcs.Task;
        }

但现在我需要使用 SFSafariViewController 使用 Dropbox OAuth API 对用户进行身份验证。当我在支持互联网的WIFI 或蜂窝网络上时,我知道如何使用 SFSafariViewController。

但是当我想打开一些 OAuth URL(下面的 OAuth URL 示例)或任何URL https ://www.google.com ) 使用 SFSafariViewController。

OAuth URL: https://www.dropbox.com/oauth2/authorize?client_id=DROPBOX_KEY&response_type=code&redirect_uri=REDIRECT_URL

请指教。谢谢。

4

0 回答 0