C# 如何在Google Plus上发布活动

C# 如何在Google Plus上发布活动,c#,google-plus,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/quest

我试过这个密码

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”或其依赖项之一。系统找不到指定的文件


您使用的是一个简单的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"
        };
    }
}
公共类AAGoogleAuthorizationCodeRequestUrl:GoogleAuthorizationCodeRequestUrl
{
[Google.api.Util.RequestParameterAttribute(“请求可见的操作”,Google.api.Util.RequestParameterType.Query)]
公共字符串访问操作{get;set;}
公共AAGOOGOLEAuthorizationCodeRequestURL(Uri授权服务器URL)
:base(authorizationServerUrl)
{
}
}
公共类AAGOOGOLEAuthorizationCodeFlow:AuthorizationCodeFlow
{
///构造一个新的Google授权代码流。
公共AAGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer初始值设定项)
:base(初始值设定项)
{
}
公共覆盖AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(字符串重定向URI)
{
返回新的AAGoogleAuthorizationCodeRequestUrl(新Uri(AuthorizationServerUrl))
{
ClientId=ClientSecrets.ClientId,
Scope=string.Join(“,Scopes),
RedirectUri=RedirectUri,
访问次数=”http://schemas.google.com/AddActivity"
};
}
}

这将授权用户进行应用程序活动(在控制台上),但您可能应该使用客户端授权。为此,,在javascript客户端和服务器上使用类似的方法并授权用户。

我假设程序集在您的bin文件夹中?您可能需要删除该API密钥。您实际上需要重置API密钥并从Google获得一个新的API密钥。我浏览了您建议的链接,但无法获得解决方案。您还需要请求应用程序活动的plus.login范围。
        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",
            });
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"
        };
    }
}