如何使用 oAuth 和 SOAP?

发布于 2024-12-22 08:13:57 字数 3334 浏览 2 评论 0原文

帮助!
我正在尝试升级可能的程序以支持 Google 在 AdWords 中使用的新 oAuth。
使用我的网站,我可以为我的用户创建 accessToken 和 accessTokenSecret 并将其存储在我的数据库中。

我的问题是当我稍后尝试使用这些凭据发出肥皂请求时。

  • 我需要从网站部分保存哪些信息?到目前为止,我只保存了 accessToken 和 accessTokenSecret。还有什么吗?
  • 如何使用 accessToken、accessTokenSecret 以及我保存的其他内容来发出 SOAP 请求? (请尽可能“低级别”,“仅在请求标头中使用它们”对我没有帮助)。

有关我的流程的一些信息:

  • 不使用 Google 的客户端库(开销太大,到目前为止我不需要它们)。
  • 在我正在使用的服务上使用 VS2005 WSDL 自动生成的 SOAP 代码。
  • C# 代码。

任何帮助将不胜感激。
谢谢!

=================================================== ======================================= 根据建议,我通过以下方式扩展了 GetWebRequest:

protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest request = base.GetWebRequest(uri);

            String token = "XXXXXXXXXXX";//a valid token - changed for here.
            String secret = "XXXXXXXXXXXX";//a valid secret - changed for here.
            String consumerKey = "anonymous";
            String consumerSecret = "anonymous";
            String sigMet = "HMAC-SHA1";
            String oauth_timestamp = ((DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) / (1000 * 10000)).ToString();
            String oauth_nonce = Guid.NewGuid().ToString();

            Parameter[] paramArray = new Parameter[]{
                                        new Parameter("oauth_consumer_key", consumerKey),
                                        new Parameter("oauth_token", token),
                                        new Parameter ("oauth_signature_method", sigMet),
                                        new Parameter ("oauth_timestamp", oauth_timestamp),
                                        new Parameter ("oauth_nonce", oauth_nonce)
                                        };

            String oauth_signature = CreateHMACSHA1Signature(
                                        request.Method,
                                        uri,
                                        paramArray,
                                        consumerSecret,
                                        secret
                                        );

            request.Headers.Add(
                            "Authorization: OAuth " +
                            "realm=\"" + "https://www.google.com/" + "\"," +
                            "oauth_consumer_key=\"" + Parameter.EncodeParameterString(consumerKey) + "\"," +
                            "oauth_token=\"" + Parameter.EncodeParameterString(token) + "\"," +
                            "oauth_signature_method=\"" + Parameter.EncodeParameterString(sigMet) + "\"," +
                            "oauth_signature=\"" + Parameter.EncodeParameterString(oauth_signature) + "\"," +
                            "oauth_timestamp=\"" + Parameter.EncodeParameterString(oauth_timestamp) + "\"," +
                            "oauth_nonce=\"" + Parameter.EncodeParameterString(oauth_nonce) + "\""
                        );

            return request;
        }

函数 CreateHMACSHA1Signature 与用于 OAuthGetRequestTokenOAuthAuthorizeToken 的函数相同。但是当与 SOAP 一起使用时,我收到以下错误:
System.Web.Services.Protocols.SoapException:AuthenticationError.OAUTH_TOKEN_HEADER_INVALID @ Service[ServicedAccountService.get]

知道为什么吗?

HELP !
I'm trying to upgrade may program to support the new oAuth that Google uses in the AdWords.
Using my website I'm able to create an accessToken and an accessTokenSecret for my users and store it in my database.

My problem is when I try to make a soap request later with those credentials.

  • Which information do I need to save from the website part? so far I save only the accessToken and the accessTokenSecret. is there anything else?
  • How do I use the accessToken, accessTokenSecret and what ever else I've saved in order to make a SOAP requests? (please be as "low level" as you can, "just use them in the request header" won't help me).

Some info on my process:

  • Not using the Client Library from Google (too much over head, and so far I didn't needed them).
  • Using the auto-generated SOAP code using VS2005 WSDL on the services I'm using.
  • C# code.

Any help would be highly appreciated.
Thanks!

=======================================================================================
As advised, I have extend the GetWebRequest in the following way:

protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest request = base.GetWebRequest(uri);

            String token = "XXXXXXXXXXX";//a valid token - changed for here.
            String secret = "XXXXXXXXXXXX";//a valid secret - changed for here.
            String consumerKey = "anonymous";
            String consumerSecret = "anonymous";
            String sigMet = "HMAC-SHA1";
            String oauth_timestamp = ((DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) / (1000 * 10000)).ToString();
            String oauth_nonce = Guid.NewGuid().ToString();

            Parameter[] paramArray = new Parameter[]{
                                        new Parameter("oauth_consumer_key", consumerKey),
                                        new Parameter("oauth_token", token),
                                        new Parameter ("oauth_signature_method", sigMet),
                                        new Parameter ("oauth_timestamp", oauth_timestamp),
                                        new Parameter ("oauth_nonce", oauth_nonce)
                                        };

            String oauth_signature = CreateHMACSHA1Signature(
                                        request.Method,
                                        uri,
                                        paramArray,
                                        consumerSecret,
                                        secret
                                        );

            request.Headers.Add(
                            "Authorization: OAuth " +
                            "realm=\"" + "https://www.google.com/" + "\"," +
                            "oauth_consumer_key=\"" + Parameter.EncodeParameterString(consumerKey) + "\"," +
                            "oauth_token=\"" + Parameter.EncodeParameterString(token) + "\"," +
                            "oauth_signature_method=\"" + Parameter.EncodeParameterString(sigMet) + "\"," +
                            "oauth_signature=\"" + Parameter.EncodeParameterString(oauth_signature) + "\"," +
                            "oauth_timestamp=\"" + Parameter.EncodeParameterString(oauth_timestamp) + "\"," +
                            "oauth_nonce=\"" + Parameter.EncodeParameterString(oauth_nonce) + "\""
                        );

            return request;
        }

the function CreateHMACSHA1Signature is the same one used to with OAuthGetRequestToken and OAuthAuthorizeToken just fine. but when used with the SOAP I'm getting the following error:
System.Web.Services.Protocols.SoapException: AuthenticationError.OAUTH_TOKEN_HEADER_INVALID @ Service[ServicedAccountService.get]

Any idea why it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-12-29 08:13:57

我已经在 .NET 客户端库的上下文中回答了这个问题,网址为 使用新的 oAuth 1.0 的 Google Ads API、C#、SOAP 请求?。但要在 NoClientLibrary 上下文中回答您的问题,

  1. 除了 OAuth 访问密钥和秘密之外,您只需要使用者密钥。
  2. 您需要对请求进行签名,就像请求普通的 OAuth 受保护资源一样。然后将签名放入 SOAP Web 请求的授权 HTTP 标头(而不是 SOAP 标头)中。然而,获取 SOAPHttpClientProtocol 对象的底层 HttpWebRequest 并不那么简单。您需要扩展 SOAPHttpClientProtocol 对象,重写受保护的 GetWebRequest 方法并在此阶段设置 OAuth 标头:类似于:

    受保护的覆盖 WebRequest GetWebRequest(Uri uri) {
      WebRequest 请求 = base.GetWebRequest(uri);
      字符串 oAuthHeader = SignTheRequestAndGetTheOAuthHeader(请求);
      if (!string.IsNullOrEmpty(oAuthHeader)) {
         request.Headers["授权"] = oAuthHeader;
      }
      退货请求;
    }
    

这也意味着您必须手动修改自动生成的代码以更改存根服务类的基类,您是从长远来看不会享受太多。另外,如果您不知道如何正常请求 OAuth 受保护的资源,相关文档位于 http://oauth.net/core/1.0/#anchor13

现在,.NET 客户端库已为您处理好这一问题。该库并不难使用,http 上有足够的 wiki 文章://code.google.com/p/google-api-adwords-dotnet/wiki 为您提供指导。我建议您使用 .NET 客户端库,但如果您选择不这样做,这里列出了您在采用 NoClientLibrary 路线时应该注意的陷阱: com/p/google-api-adwords-dotnet/wiki/NoClientLibrary" rel="nofollow noreferrer">http://code.google.com/p/google-api-adwords-dotnet/wiki/NoClientLibrary。

我还想提一下,AdWords API 官方论坛位于 http://groups .google.com/group/adwords-api?pli=1,我经常在那里回答开发者问题。如果您有任何后续问题,请随时在那里提问,我很乐意回答您的问题。

干杯,
阿纳什

I've answered this question here in the context of the .NET client library at Google Ads API, C#, SOAP request with new oAuth 1.0?. But to answer your question in a NoClientLibrary context,

  1. You just need the consumer key in addition to OAuth access key and secret.
  2. You need to sign the request as if you were requesting a normal OAuth protected resource. Then put the signature in Authorization HTTP header (not the SOAP header) of your SOAP web request. However, getting hold of the underlying HttpWebRequest for a SOAPHttpClientProtocol object is not that straightforward. You need to extend SOAPHttpClientProtocol object, override the protected GetWebRequest method and set your OAuth headers at this stage: Something like:

    protected override WebRequest GetWebRequest(Uri uri) {
      WebRequest request = base.GetWebRequest(uri);
      string oAuthHeader = SignTheRequestAndGetTheOAuthHeader(request);
      if (!string.IsNullOrEmpty(oAuthHeader)) {
         request.Headers["Authorization"] = oAuthHeader;
      }
      return request;
    }
    

This also means that you have to manually modify the autogenerated code to change the base class of your stub service classes, something you are not going to enjoy much in the long run. Also, in case you don't know how to normally request an OAuth protected resource, the relevant documentation is at http://oauth.net/core/1.0/#anchor13.

Now, this is something that has been taken care of for you in the .NET client library. The library is not that difficult to use, there are enough wiki articles at http://code.google.com/p/google-api-adwords-dotnet/wiki to guide you. I recommend that you use the .NET client library, but in case you choose not to do so, here's a list of pitfalls you should be aware of when taking the NoClientLibrary route: http://code.google.com/p/google-api-adwords-dotnet/wiki/NoClientLibrary.

I also wanted to mention that AdWords API official discussion forum is at http://groups.google.com/group/adwords-api?pli=1, and I frequently answer developer questions there. If you have any followup questions, feel free to ask there and I'll be happy to answer your questions.

Cheers,
Anash

你的往事 2024-12-29 08:13:57

成功 !!!

我很好地发现了问题所在,而且这只是偶然的。
创建签名时,我使用了 request.method 作为方法参数。
问题在于,此时方法是“GET”,但 SOAP 机制会将其更改为“POST”(因此它可以传递 SOAP 参数),这导致我的签名不起作用。

我需要做的唯一修复是将:更改

String oauth_signature = CreateHMACSHA1Signature(
    request.Method,
    ...
    )

为:

String oauth_signature = CreateHMACSHA1Signature(
    "POST",
    ...
    )

SUCCESS !!!

I finely found what was wrong, and it was only by a chance.
when creating the signature I've used the request.method for the method parameter.
The problem with this is that at this moment the method is "GET" but the SOAP mechanisim will change it to "POST" (so it can pass the SOAP parameters) this cause my signature to not work.

the only fix I needed to do is change:

String oauth_signature = CreateHMACSHA1Signature(
    request.Method,
    ...
    )

to:

String oauth_signature = CreateHMACSHA1Signature(
    "POST",
    ...
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文