基于 App Engine 的 OAuth 访问问题

在 App Engine 上使用 OAuth 进行资源访问时遇到了一些困难。客户端应用程序(基于 Linux,使用 python-oauth)可以获取有效的“访问令牌”,但当我尝试访问受保护的资源时(例如:user = oauth.get_current_user()),会引发 oauth.OAuthRequestError 异常。

以下是请求头信息:

headers: {'Content-Length': '21', 'User-Agent': 'musync,gzip(gfe),gzip(gfe)', 'Host': 'services.systemical.com', 'X-Google-Apps-Metadata': 'domain=systemical.com', 'X-Zoo': 'app-id=services-systemical,domain=systemical.com', 'Content-Type': 'application/json', 'Authorization': 'OAuth oauth_nonce="03259912", oauth_timestamp="1282928181", oauth_consumer_key="services.systemical.com", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_token="1%2Fo0-tcGTfRzkkm449qVxd_8CfCvMcW_0xwL024nO3HgI", oauth_signature="2ojSK6Ws%2BvDxx3Rdlltf53hlI2w%3D"'}

我怀疑这个问题可能与域名有关,即我正在使用 services.systemical.com 上的端点,而我看到 App Engine 为 X-Google-Apps-Metadata 报告了 domain=systemical.com。

我该怎么解决这个问题?这是我如何将子域名与 Apps/App Engine 一起使用的问题吗?

域名“services.systemical.com”指向(DNS CNAME)我的 appengine 应用程序 services-systemical.appspot.com。域名 systemical.com 与 Google Apps 域名相关联。

更新:以下是使用的客户端代码:

class OauthClient(object):

    gREQUEST_TOKEN_URL = 'OAuthGetRequestToken'
    gACCESS_TOKEN_URL =  'OAuthGetAccessToken'
    gAUTHORIZATION_URL = 'OAuthAuthorizeToken'

    def __init__(self, server, port, base):
        self.server=server
        self.port=port
        self.base=base
        self.request_token_url=self.base+self.gREQUEST_TOKEN_URL
        self.access_token_url=self.base+self.gACCESS_TOKEN_URL
        self.authorize_token_url=self.base+self.gAUTHORIZATION_URL
        self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port))

    def fetch_request_token(self, oauth_request):
        self.connection.request(oauth_request.http_method, self.request_token_url, headers=oauth_request.to_header()) 
        response = self.connection.getresponse()
        print response.status, response.reason
        print response.msg
        return oauth.OAuthToken.from_string(response.read())

    def fetch_access_token(self, oauth_request):
        self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header()) 
        response = self.connection.getresponse()
        return oauth.OAuthToken.from_string(response.read())

    def authorize_token(self, oauth_request):
        self.connection.request(oauth_request.http_method, oauth_request.to_url()) 
        response = self.connection.getresponse()
        return response.read()

2. 解决方案

要解决这个问题,需要确保以下几点:

  1. 确保域名“services.systemical.com”已添加到 Google Apps 域名的授权域名列表中。
  2. 确保 App Engine 应用程序已使用正确的域名进行配置。
  3. 确保正在使用正确的 OAuth 请求签名方法。

以下是一些代码示例,演示了如何使用 OAuth 进行资源访问:

import oauth
import httplib

# 填写您的应用密钥和令牌
consumer_key = "YOUR_CONSUMER_KEY"
consumer_secret = "YOUR_CONSUMER_SECRET"
access_token = "YOUR_ACCESS_TOKEN"
access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"

# 创建 OAuth 消费者
consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)

# 创建 OAuth 令牌
token = oauth.OAuthToken(access_token, access_token_secret)

# 创建 OAuth 请求
request = oauth.OAuthRequest.from_consumer_and_token(consumer, 
                                                    token, "GET", "https://example.com/api/v1/resource")

# 签名请求
request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, token)

# 发送请求
connection = httplib.HTTPConnection("example.com")
connection.request(request.http_method, request.to_url())
response = connection.getresponse()

# 处理响应
print(response.status, response.reason)
print(response.read())

通过这些步骤,您应该能够在 App Engine 上成功使用 OAuth 进行资源访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值