【问题标题】:Basic Google API Oauth2 Authentication Setup Fails To Give Objects?基本的 Google API Oauth2 身份验证设置无法提供对象?
【发布时间】:2018-11-04 06:21:57
【问题描述】:

我编写了一个身份验证代码并获得了一个访问令牌,但是当我使用它来抓取一个对象时,我最终得到了'NoneType' object is not callable。

 Exception Location: googleads\adwords.py in GetService, line 365
 Exception Type: Type Error
 Exception Value: Internal Server Error: /oauth2callback

无论调用 Customer 还是 CampaignService,我都会得到相同的结果。我不明白我做错了什么。

我正在关注Googleads中的代码。

def getAdwordsFlow():
    FLOW.redirect_uri = 'http://localhost/oauth2callback'

    # Generate URL for request to Google's OAuth 2.0 server.
    authorization_url, state = FLOW.authorization_url(
        access_type='offline',
        include_granted_scopes='true')
    return authorization_url


def getAdwordsTokens(request):
    auth_code = request.GET.get('code')
    FLOW.fetch_token(code=auth_code)
    credentials = FLOW.credentials

    oauth2_client = oauth2.GoogleAccessTokenClient(
    FLOW.credentials.token, FLOW.credentials.expiry)

    adwords_client = adwords.AdWordsClient(
    "DEVELOPER-TOKEN", oauth2_client, "USER-AGENT", "CLIENT-CUSTOMER-ID")

    customer = adwords_client.GetService('CustomerService').getCustomers()[0]
    print('You are logged in as customer: %s' % customer['customerId'])

    return HttpResponseRedirect("/")

url.py

urlpatterns = [
    re_path(r'^oauth2callback', queries.getAdwordsTokens, name='auth_calledback'),] #How 

view.py

def index(request):
    return redirect(getAdwordsFlow())

终端输出:

"GET /oauth2callback?state=XXXXXXXXX&code=4/XXXXXXXXXXX&scope=https://www.googleapis.com/auth/adwords+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email HTTP/1.1" 500 80213 为什么是500?

我注意到我的访问令牌在我调用它时具有不同的值。所以我假设它正在工作。

【问题讨论】:

    标签: python django oauth-2.0 google-ads-api google-api-python-client


    【解决方案1】:

    根据您的问题,您的服务电话是-

    GET /oauth2callback?state=XXXXXXXXX&code=4/XXXXXXXXXXX&scope=https://www.googleapis.com/auth/adwords+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.emailHTTP/1.1

    此调用既不是获取访问令牌调用,也不是在请求中包含access_token,根据您的reference 代码,它将从refresh_token 生成access_token。

    def main(access_token, token_expiry, client_customer_id, developer_token,
             user_agent):
      oauth2_client = oauth2.GoogleAccessTokenClient(access_token, token_expiry)
    
      adwords_client = adwords.AdWordsClient(
          developer_token, oauth2_client, user_agent,
          client_customer_id=client_customer_id)
    
      customer = adwords_client.GetService('CustomerService').getCustomers()[0]
      print 'You are logged in as customer: %s' % customer['customerId']
    
    
    if __name__ == '__main__':
      args = parser.parse_args()
    
      # Retrieve a new access token for use in this example. In a production
      # application, you may use a credential store to share access tokens for a
      # given user across applications.
      oauth2credentials = client.OAuth2Credentials(
          None, args.client_id, args.client_secret, args.refresh_token,
          datetime.datetime(1980, 1, 1, 12), GOOGLE_OAUTH2_ENDPOINT,
          USER_AGENT)
    
      oauth2credentials.refresh(httplib2.Http())
    
      main(oauth2credentials.access_token, oauth2credentials.token_expiry,
           args.client_customer_id, args.developer_token, USER_AGENT)
    

    所以要使用您的代码,首先使用this code 生成refresh_token,然后在您给定的代码中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 2014-01-23
      • 2015-11-21
      • 2021-07-29
      • 2013-06-24
      • 2015-11-30
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多