如何在Google Plus上发布活动

时间:2014-07-23 13:39:08

标签: c# google-plus

我试过这段代码

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

,错误消息是

  

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

1 个答案:

答案 0 :(得分:0)

您使用的是一个不会授权用户的简单API密钥。如Google+ Photohunt sample中所示,对用户进行签名,然后您就可以编写应用活动。

对于授权步骤,请按照此处的说明授权用户:

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"
        };
    }
}

这将授权用户进行应用程序活动(在控制台上),但您可能应该使用客户端授权。为此,请使用the Google+ quickstart sample之类的内容,并在javascript客户端和服务器上对用户进行授权。