2013-03-07 111 views
1

我正在尝试使用dotnet-client在Google+中编写活动。问题是,我似乎无法正确地获得我的客户端应用程序的配置。根据Google+ Sign-In configurationthis SO question,我们需要添加requestvisibleactions参数。我做到了,但没有奏效。我正在使用范围https://www.googleapis.com/auth/plus.login,我甚至添加了范围https://www.googleapis.com/auth/plus.moments.write,但插入仍然无效。Google+插入时刻使用google-api-dotnet-client

这是我的请求的URL是什么样子:

https://accounts.google.com/ServiceLogin?service=lso&passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?scope%3Dhttps://www.googleapis.com/auth/plus.login%2Bhttps://www.googleapis.com/auth/plus.moments.write%26response_type%3Dcode%26redirect_uri%3Dhttp://localhost/%26state%3D%26requestvisibleactions%3Dhttp://schemas.google.com/AddActivity%26client_id%3D000.apps.googleusercontent.com%26request_visible_actions%3Dhttp://schemas.google.com/AddActivity%26hl%3Den%26from_login%3D1%26as%3D-1fbe06f1c6120f4d&ltmpl=popup&shdf=Cm4LEhF0aGlyZFBhcnR5TG9nb1VybBoADAsSFXRoaXJkUGFydHlEaXNwbGF5TmFtZRoHQ2hpa3V0bwwLEgZkb21haW4aB0NoaWt1dG8MCxIVdGhpcmRQYXJ0eURpc3BsYXlUeXBlGgdERUZBVUxUDBIDbHNvIhTeWybcoJ9pXSeN2t-k8A4SUbfhsygBMhQivAmfNSs_LkjXXZ7bPxilXgjMsQ&scc=1

你可以从这里看到,有一个request_visible_actions,我甚至增加了一个有没有底线的情况下,我得到的参数错误( requestvisibleactions)。

让我说,我的应用程序正在通过API成功验证。我可以在通过身份验证后获取用户的个人资料,并且它位于我的应用失败的“插入时刻”部分。我的插入代码:

 var body = new Moment(); 
     var target = new ItemScope(); 
     target.Id = referenceId; 
     target.Image = image; 
     target.Type = "http://schemas.google.com/AddActivity"; 
     target.Description = description; 
     target.Name = caption; 

     body.Target = target; 
     body.Type = "http://schemas.google.com/AddActivity"; 

     var insert = 
      new MomentsResource.InsertRequest(
       // this is a valid service instance as I am using this to query the user's profile 
       _plusService, 
       body, 
       id, 
       MomentsResource.Collection.Vault); 

     Moment result = null; 
     try 
     { 
      result = insert.Fetch(); 
     } 
     catch (ThreadAbortException) 
     { 
      // User was not yet authenticated and is being forwarded to the authorization page. 
      throw; 
     } 
     catch (Google.GoogleApiRequestException requestEx) 
     {     
      // here I get a 401 Unauthorized error 

     } 
     catch (Exception ex) 
     { 

     }   ` 
+1

您是否使用了页面上的Google+登录按钮?如果是这样,你可以将该标记粘贴到你的文章中?或者您是否使用原生OAuth来触发您显示的网址?我相信,从URL的形成方式来看,您并未使用该按钮来触发身份验证,是否有原因? – BrettJ 2013-03-07 15:25:45

+0

我正在使用dotnet客户端。我展示的URL是由API构建的。我知道dotnet客户端使用DotNetOpenAuth(DNOA),所以我认为这是建立URL的DNOA。身份验证通过'Google.Apis.Authentication.OAuth2.OAuth2Authenticator'完成。 – 2013-03-08 04:22:44

+0

哦,对不起,我错过了回答你的一个问题。我使用我自己的链接/按钮,在那里我调用一个启动身份验证的方法。我的验证码基于样本。如果需要,我也可以发布它。 – 2013-03-08 04:39:59

回答

1

啊就这么简单!也许不会?我正在回答我自己的问题,并因此接受它作为答案(在几天之后),以便引导具有相同问题的其他人。但是我肯定会为Gus的回答投票,因为这导致我修复了我的代码。

因此,根据上面所写的@class回答和explained on his blog,成功创建片刻的关键是添加request_visible_actions参数。我这样做了,但我的请求仍然失败,这是因为我错过了一件重要的事情。您需要添加一个参数,那就是access_type,它应该设置为offline。 OAuth请求至少应如下所示:https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/plus.login&response_type=code&redirect_uri=http://localhost/& request_visible_actions = http://schemas.google.com/AddActivity&access_type=offline

对于完整和正确的客户代码,您可以get Gus' example heredownload the entire dotnet client library including the source and sample并添加我在下面添加的内容。您应该记住的最重要的事情是修改Google API的AuthorizationServerDescription。这里是我的版本认证的:

public static OAuth2Authenticator<WebServerClient> CreateAuthenticator(
     string clientId, string clientSecret) 
{ 
    if (string.IsNullOrWhiteSpace(clientId)) 
     throw new ArgumentException("clientId cannot be empty"); 

    if (string.IsNullOrWhiteSpace(clientSecret)) 
     throw new ArgumentException("clientSecret cannot be empty"); 

    var description = GoogleAuthenticationServer.Description; 

    var uri = description.AuthorizationEndpoint.AbsoluteUri; 
    // This is the one that has been documented on Gus' blog site 
    // and over at Google's (https://developers.google.com/+/web/signin/) 
    // This is not in the dotnetclient sample by the way 
    // and you need to understand how OAuth and DNOA works. 
    // I had this already, see my original post, 
    // I thought it will make my day. 
    if (uri.IndexOf("request_visible_actions") < 1) 
    { 
     var param = (uri.IndexOf('?') > 0) ? "&" : "?"; 
     description.AuthorizationEndpoint = new Uri(
      uri + param + 
      "request_visible_actions=http://schemas.google.com/AddActivity"); 
    } 

    // This is what I have been missing! 
    // They forgot to tell us about this or did I just miss this somewhere? 
    uri = description.AuthorizationEndpoint.AbsoluteUri; 
    if (uri.IndexOf("offline") < 1) 
    { 
     var param = (uri.IndexOf('?') > 0) ? "&" : "?"; 
     description.AuthorizationEndpoint = 
       new Uri(uri + param + "access_type=offline"); 
    } 

    // Register the authenticator. 
    var provider = new WebServerClient(description) 
    { 
     ClientIdentifier = clientId, 
     ClientSecret = clientSecret,     
    };    
    var authenticator = 
     new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) 
       { NoCaching = true }; 
    return authenticator; 
} 

没有access_type=offline我的代码从未工作而它永远不会工作。现在我想知道为什么?有一些解释会很好。

2

对于OAuth的流动,有两个问题你的要求:

  • request_visible_actions是什么是传递到OAuth的V2服务器(不及格requestvisibleactions)
  • plus.moments.write是已弃用的范围,您只需要传入plus.login

确保您的项目引用了最新版本的Google+ .NET客户端库f ROM这里:

https://developers.google.com/resources/api-libraries/download/stable/plus/v1/csharp

我已经创建了GitHub上一个项目,在这里展示一个完整的服务器端流量:

https://github.com/gguuss/gplus_csharp_ssflow

正如Brettj说,你应该使用Google+登录按钮的最新Google+样品中表现出在这里:

https://github.com/googleplus/gplus-quickstart-csharp

首先,确保您正在请求所有您正在编写的活动类型。您将知道这是行得通的,因为授权对话框将显示“通过Google提供您的应用程序活动,您可以看到并且:[...]”以“此应用程序想要”开头的文本下方。我知道你检查了这个,但我90%确定这就是你得到401错误代码的原因。以下标记显示了如何呈现请求访问“添加”活动的Google+登录按钮。

<div id="gConnect"> 
<button class="g-signin" 
    data-scope="https://www.googleapis.com/auth/plus.login" 
    data-requestvisibleactions="http://schemas.google.com/AddActivity" 
    data-clientId="YOUR_CLIENT_ID" 
    data-accesstype="offline" 
    data-callback="onSignInCallback" 
    data-theme="dark" 
    data-cookiepolicy="single_host_origin"> 
</button> 

假设你有一个在数据requestvisibleactions设置正确的活动类型PlusService对象,下面的代码,你应该能够复制/粘贴以了解其工作,简明地演示如何使用.NET编写的时刻客户端和经过测试的工作:

Moment body = new Moment(); 
ItemScope target = new ItemScope(); 

target.Id = "replacewithuniqueforaddtarget"; 
target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png"; 
target.Type = ""; 
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(
     _plusService, 
     body, 
     "me", 
     MomentsResource.Collection.Vault); 
Moment wrote = insert.Fetch(); 

注意,我包括Google.Apis.Plus.v1.Data为了方便。

+0

HI @class我通过'requestvisibleactions'传入希望它可以工作,我想也许arg名称改变了。我实际上尝试了两种方法:(1)使用'requestvisibleactions'和(2)使用'request_visible_actions'并且没有任何工作。此外,您给我的片段与我所拥有的片段相同,除了:(1)target.Type,其中我尝试使用“AddActivity”且没有,两者都不起作用。 (2)“我”,我的“id”等于“我”。我们有相同的代码,但它不适合我。它导致我认为dotnet客户端上没有正确包含requestvisibleactions参数的东西缺失? – 2013-03-08 04:31:16

+0

另外,让我再说一遍,我的代码用于登录并获取用户的配置文件。所以我确定我的身份验证只是缺少一个配置,所以我可以创建一个活动。顺便说一句,我的第一次尝试没有'plus.moments.write'范围,它没有工作,这导致我加入它 - 我想也许它会工作。没有快乐。 – 2013-03-08 04:34:30

+0

@vonv。我将我的项目添加到GitHub,希望这可以帮助您调试。确保你只传递request_visible_actions。当您看到用户界面文字变化表示正在请求应用程序活动时,您会知道您的授权请求是正确的。另外,要仔细检查这里,你正在做一个服务器端的流程?导致此问题的最后一件事是,您有一个较旧的库,并在答案中添加了一个链接。 – class 2013-03-08 04:48:59