代码之家  ›  专栏  ›  技术社区  ›  madtyn

403在Python中使用Google+API时未经授权

  •  0
  • madtyn  · 技术社区  · 8 年前

    我使用的代码主要是从谷歌样本中提取的:

    """
    did a pip3 install --upgrade google-api-python-client
    """
    
    import httplib2
    
    from apiclient.discovery import build
    from oauth2client.client import OAuth2WebServerFlow
    
    from environments import get_client_id, get_client_secret
    
    # List the scopes your app requires:
    SCOPES = ['https://www.googleapis.com/auth/plus.me',
              'https://www.googleapis.com/auth/plus.stream.write']
    
    # The following redirect URI causes Google to return a code to the user's
    # browser that they then manually provide to your app to complete the
    # OAuth flow.
    REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
    
    # For a breakdown of OAuth for Python, see
    # https://developers.google.com/api-client-library/python/guide/aaa_oauth
    # CLIENT_ID and CLIENT_SECRET come from your API Console project
    flow = OAuth2WebServerFlow(client_id=get_client_id(),  # extracted from console.developers.google.com
                               client_secret=get_client_secret(), # extracted from console.developers.google.com
                               scope=SCOPES,
                               redirect_uri=REDIRECT_URI)
    
    auth_uri = flow.step1_get_authorize_url()
    
    # This command-line server-side flow example requires the user to open the
    # authentication URL in their browser to complete the process. In most
    # cases, your app will use a browser-based server-side flow and your
    # user will not need to copy and paste the authorization code. In this
    # type of app, you would be able to skip the next 3 lines.
    # You can also look at the client-side and one-time-code flows for other
    # options at https://developers.google.com/+/web/signin/
    print('Please paste this URL in your browser to authenticate this program.')
    print(auth_uri)
    code = input('Enter the code it gives you here: ')
    
    # Set authorized credentials
    credentials = flow.step2_exchange(code)
    
    # Create a new authorized API client.
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('plusDomains', 'v1', http=http)
    
    circle_service = service.circles()
    request = circle_service.list(userId='me')
    
    while request is not None:
        circle_list = request.execute()
    
        if circle_list.get('items') is not None:
            print('Google+ circles for the current user: ')
            circles = circle_list.get('items')
            for circle in circles:
                print('\t %s' % circle.get('displayName'))
    
        request = circle_service.list_next(request, circle_list)
    

    您可以看到,我有一些函数返回从 Google's developers console .

    credentials_console_screenshot

    我使用右侧的红色下载按钮下载了用于client\u id的红色和*-掩码字符串以及JSON中的一个client\u秘密。

    首先一切都很好,它在Chrome中打开了一个窗口,我同意并将代码粘贴到命令行,当谈到这段代码时:

    request.execute()
    

    它显示了错误跟踪:

    Traceback (most recent call last):
      File "/home/madtyn/PycharmProjects/telegram/api_samples/plus/sample_list_circles.py", line 62, in <module>
        circle_list = request.execute()
      File "/home/madtyn/.local/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
        return wrapped(*args, **kwargs)
      File "/home/madtyn/.local/lib/python3.6/site-packages/googleapiclient/http.py", line 842, in execute
        raise HttpError(resp, content, uri=self.uri)
    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/plusDomains/v1/people/me/circles?alt=json returned "Forbidden">
    

    我用基本的G+API编写了一个类似的代码,并使其正常工作,但在这方面我陷入了困境。我不知道哪里出错了。我得到的唯一线索是,这个导入在PyCharm中显示为未解析:

    from apiclient.discovery import build
    

    有什么想法吗?我找不到我没有遵循谷歌API指令的地方。我一直在读,但我一点线索也没有。

    1 回复  |  直到 8 年前
        1
  •  0
  •   madtyn    8 年前

    我接受这个答案,但如果有人真的提出了解决方案,我想我可以改变被接受的答案,所以,请便。