【问题标题】:how to insert a moment on google plus using c#如何使用 c# 在 google plus 上插入片刻
【发布时间】:2014-08-13 10:36:13
【问题描述】:

我正在尝试这段代码:

PlusService plus = new PlusService(
       new Google.Apis.Services.BaseClientService.Initializer()
       {
           ApiKey = "AIzaSyDWaaG1Hnjgjgho6PVdefefddsdsC6FlPXv5rommyzCAf0ziHkTo"
       });
Moment body = new Moment();
ItemScope target = new ItemScope();            
           target.Id = "103692090756564666566349707";
target.Description = "The description for the activity";
target.Name = "An example of add activity";
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";           
MomentsResource.InsertRequest insert = new MomentsResource.InsertRequest(
    plus, 
    body, 
    "1036920323290766566566349707",
MomentsResource.InsertRequest.CollectionEnum.Vault);
var momentsResource = plus.Activities.List(
    "me", 
    ActivitiesResource.ListRequest.CollectionEnum.Public);
Moment wrote = insert.Execute();//Error here

我的 scop 脚本是 -

$(location).attr('href', 'https://accounts.google.com/o/oauth2/auth?scope=' +
             'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.login&https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me&' +
          'state=generate_a_unique_state_value&' +
          'redirect_uri=http://localhost:58925/Home/&' +
          'response_type=code&' +
          'client_id=564565402546260119.apps.googleusercontent.com&' +
          'access_type=offline');

错误信息是—— 无法加载文件或程序集“Zlib.Portable,Version=1.9.1.9000,Culture=neutral,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件。

【问题讨论】:

标签: c# google-plus


【解决方案1】:

首先,您没有授权用户请求 Google+ 范围。您的服务初始化程序需要如下所示:

        UserCredential credential;

        var initializer = new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = secrets,
            Scopes = new[] { PlusService.Scope.PlusLogin}
        };
        var flow = new AAGoogleAuthorizationCodeFlow(initializer);
        credential = await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync
            ("user", CancellationToken.None).ConfigureAwait(false);

        // Create the service.
        var service = new PlusService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

Google+ Quickstart sample 展示了使用 Google+ 登录的官方方式。登录用户后,您将能够使用 C# 客户端库编写时刻,例如:

        Moment body = new Moment();
        ItemScope itemScope = new ItemScope();
        itemScope.Id = "replacewithuniqueforaddtarget";
        itemScope.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
        itemScope.Type = "";
        itemScope.Description = "The description for the action";
        itemScope.Name = "An example of add activity";
        body.Object = itemScope;
        body.Type = "http://schema.org/AddAction";
        MomentsResource.InsertRequest insert =
            new MomentsResource.InsertRequest(
                service,
                body,
                "me",
                MomentsResource.InsertRequest.CollectionEnum.Vault);

        Moment wrote = insert.Execute();

        Console.WriteLine("Wrote app activity:\n" + wrote.Result);

如果您尝试从控制台应用程序执行此操作,您可以从 GitHub C# Console demo solution 开始。

【讨论】:

  • 感谢您的建议,但它在 credential = await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync ("user", CancellationToken.None).ConfigureAwait(false) ;无法加载文件或程序集“Microsoft.Threading.Tasks.Extensions.Desktop,Version=1.0.16.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。系统找不到指定的文件。
  • 这是 BCL / async 的一个已知问题。见:stackoverflow.com/questions/21057052/…
猜你喜欢
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多