如何使用Xamarin中现有的访问令牌来使用Google Apis? [英] How to use Google Apis using existing Access Token in Xamarin?

查看:90
本文介绍了如何使用Xamarin中现有的访问令牌来使用Google Apis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xamarin.auth在Xamarin表单应用程序中成功实现了登录过程.现在,我想连接到Google API并上传AppData.这是我尝试过的代码, 我努力使用令牌来获取GoogleCredential并将此凭据提供给Google API,但失败了.

I implemented login process successfully in my Xamarin forms app using Xamarin.auth. now I want to connect to Google APIs and upload AppData. here is the Code I tried, I tread to fetch the GoogleCredential using token and providing this Credential to Google API but it failed.

var store = AccountStore.Create();
                var SavedAccount = store.FindAccountsForService(GoogleDriveBackup.Auth.Constants.AppName).FirstOrDefault();


                GoogleCredential credential2 = GoogleCredential.FromAccessToken(SavedAccount.Properties["access_token"]);
                var driveService = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential2,
                    ApplicationName = "myApp",
                });
                FilesResource.CreateMediaUpload request;
                var filePath = Path.Combine(path, filename);
                using (var stream = new System.IO.FileStream(filePath,
                    System.IO.FileMode.Open))
                {
                    request = driveService.Files.Create(
                        fileMetadata, stream, "application/json");
                    request.Fields = "id";
                    request.Upload();
                }
                var file = request.ResponseBody;

request.ResponseBody始终为null.我认为这与凭证有关.

request.ResponseBody is always null. I thought that it has something to do with credentials.

我尝试使用

  var store = AccountStore.Create();
            var SavedAccount = store.FindAccountsForService(GoogleDriveBackup.Auth.Constants.AppName).FirstOrDefault();


            var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "xxxx-xxx.apps.googleusercontent.com",
                    ClientSecret = "xxxxxxxx"
                }
            });

            Google.Apis.Auth.OAuth2.Responses.TokenResponse responseToken = new Google.Apis.Auth.OAuth2.Responses.TokenResponse()
            {
                AccessToken = SavedAccount.Properties["access_token"],
                ExpiresInSeconds = Convert.ToInt64(SavedAccount.Properties["expires_in"]),
                RefreshToken = SavedAccount.Properties["refresh_token"],
                Scope = DriveService.Scope.DriveAppdata,
                TokenType = SavedAccount.Properties["token_type"],
            };
            var token= SavedAccount.Properties["access_token"];

            var credential = new UserCredential(flow, "", responseToken);

但是上述情况需要我没有的客户端机密",因为我在Google控制台中创建了"Android应用"并在ClientId上使用进行了登录.因此,我读到某个地方应该在Google控制台中创建其他"并从那里使用ClientId和Client Secret,这对我来说意义不大,因为我使用不同的客户端ID登录.无论如何,我也尝试过,但是响应为空. 那么这是怎么回事呢?如何实现我的目标?

But above case requires Client Secret which I don't have as I created "Android App" in the google console and signed in using on ClientId. So I read somewhere that I should create "Others" in the google console and use ClientId and Client Secret from there which makes not much sense to me because I am logged in with different client id's. Anyway, I tried that also but the response was null. So what is the deal here? How can achieve my goal?

推荐答案

google .net客户端库不支持xamarin.实际上,我很惊讶您能做到这一点.您将要遇到的主要问题是身份验证,因为您已经注意到.net客户端库的凭据类型将是浏览器,本机或api密钥.移动(Android Ios)客户端无法正常工作,因为您不知道身份验证的方法是不同的,并且客户端库没有执行此操作的能力.

The google .net client library doesn't support xamarin. I am actually surprised you got it working this far. The main issue you are going to have is the authentication as you have already noticed the credential type for the .net client library is going to be either browser, native or api key. The mobile (Android Ios) clients arnt going to work as you dont have a secret the method of authentication is different and the client library doesn't have ability to do this.

我唯一的建议是自己使用xamarin进行Oauth2身份验证,然后像现在一样构建TokenResponse.然后,如果您可以将dll导入项目,则可以将该令牌提供给Google .net客户端库.

The only suggestion i would have would be to work out Oauth2 authentication with xamarin on your own and then build the TokenResponse as you are doing now. You may then be able to feed that token to the Google .net client library if you can get the dlls into your project.

据我所知,我们没有计划在不久的将来使用Google .net clinet库支持xamarin,请参阅 840 1167

To my knowlage we have no plans to support xamarinwith the Google .net clinet library in the near future please see 984 840 1167

这篇关于如何使用Xamarin中现有的访问令牌来使用Google Apis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆