从C#分享GooglePlus中的时刻

时间:2014-03-25 04:04:01

标签: c# asp.net google-plus

C#中是否有任何图书馆将分享时刻的过程包含在用户的Google+帐户(或其流媒体)中?我正在寻找一些只需要你的ClientId和ClientSecret的东西,也许你的apiKey和用户的id一起发送一些用户决定与他/她的朋友分享的文字。

如果没有,但你有一个创建WebRequest来完成同样事情的例子,那也非常感激!

我已查看此着陆页:https://developers.google.com/+/quickstart/csharp

但是我正在尝试集成到已经安装了GooglePlus Auth的现有MVC5应用程序中。

1 个答案:

答案 0 :(得分:1)

用于Google API的正确客户端是Google .NET API Client library,可通过NuGet获取。如果您使用的核心库不多,则需要特定API的附加库。对于Plus,您需要 Google.Apis.Plus.v1 包。

将它添加到项目并配置了API客户端后,编写应用程序活动就像以下一样简单:

    /// <summary>The app activity type for ADD.</summary>
    private const string ADD_ACTIVITY_TYPE = @"http://schemas.google.com/AddActivity";

    // Construct your Plus Service, I'll assume a helper for here.
    PlusService plusService = GetPlusService(credentials);
    Moment addMoment = new Moment();
    ItemScope target = new ItemScope()
        {
            Url = ContentUrl
        };
    addMoment.Type = ADD_ACTIVITY_TYPE;
    addMoment.Target = target;

    Moment response = null;
    try
    {
        response = plusService.Moments.Insert(addMoment, "me",
            MomentsResource.InsertRequest.CollectionEnum.Vault).Execute();
    }
    catch (System.AggregateException)
    {
        /* Occurs when the server can't be seen by Google. */
    }
    catch (Google.GoogleApiException)
    {
        /* Occurs when the server can't be seen by Google. */
    }

如何对用户进行身份验证并授权客户端访问MVC中的Google API,请访问此博客:ASP.NET MVC with OpenID and OAuth

最后一点,应用程序活动要求您指定应用程序活动伪范围(request_visible_actions),使用“登录”按钮比通过框架更容易。如果你得到401错误,这是最可能的罪魁祸首。