如何在Google Plus上发布活动

本文关键字:布活动 活动 Google Plus | 更新日期: 2023-09-27 18:14:28

我试过这个代码

PlusService plus = new PlusService(
    new Google.Apis.Services.BaseClientService.Initializer()
    {
        ApiKey = "..."
    });
Moment body = new Moment();
ItemScope target = new ItemScope();
target.Url = "http://stackoverflow.com/questions/17543726/google-api-moments-error-google-googleapiexception";
target.Id = "103692090766566349707";
target.Description = "The description for the activity";
target.Name = "An example of add activity";
body.Target = target;
body.Type = "http://schemas.google.com/AddActivity";
//var m = plus.Moments.Insert(body,"me", MomentsResource.InsertRequest.CollectionEnum.Vault).Execute();
MomentsResource.InsertRequest insert = 
    new MomentsResource.InsertRequest(plus, body, 
        "103692090766566349707", MomentsResource.InsertRequest.CollectionEnum.Vault);
var momentsResource = plus.Activities.List("me", ActivitiesResource.ListRequest.CollectionEnum.Public);
Moment wrote = insert.Execute();// error Here

,错误信息是

无法加载文件或程序集。Portable, Version=1.9.1.9000, Culture=neutral, PublicKeyToken=null'或其依赖项之一。系统找不到指定的文件

如何在Google Plus上发布活动

您正在使用一个简单的API密钥,它不会授权用户。像Google+ Photohunt示例中演示的那样登录用户,然后您将能够编写应用程序活动。

对于授权步骤,授权用户,如下所示:

https://github.com/gguuss/google-dotnet-demo/blob/master/AuthWithAppActivities/Program.cs

    // These come from the APIs console:
    //   https://code.google.com/apis/console
    public static ClientSecrets secrets = new ClientSecrets()
    {
        ClientId = "YOUR_CLIENT_ID",
        ClientSecret = "YOUR_CLIENT_SECRET"
    };

        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,
                ApplicationName = "Gus API",
            });

还需要修改授权URL处理程序以添加应用程序活动类型:

public class AAGoogleAuthorizationCodeRequestUrl : GoogleAuthorizationCodeRequestUrl
{
    [Google.Apis.Util.RequestParameterAttribute("request_visible_actions", Google.Apis.Util.RequestParameterType.Query)]
    public string VisibleActions { get; set; }
    public AAGoogleAuthorizationCodeRequestUrl(Uri authorizationServerUrl)
        : base(authorizationServerUrl)
    {
    }
}
public class AAGoogleAuthorizationCodeFlow : AuthorizationCodeFlow
{
    /// <summary>Constructs a new Google authorization code flow.</summary>
    public AAGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer)
        : base(initializer)
    {
    }
    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
    {
        return new AAGoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
        {
            ClientId = ClientSecrets.ClientId,
            Scope = string.Join(" ", Scopes),
            RedirectUri = redirectUri,
            VisibleActions = "http://schemas.google.com/AddActivity"
        };
    }
}

这将授权用户进行应用程序活动(在控制台上),但您可能应该使用客户端授权。为此,使用类似Google+快速入门示例的东西,并在javascript客户端和服务器上同时授权用户。