【问题标题】:Can't get authentication code无法获取验证码
【发布时间】:2015-12-11 06:08:35
【问题描述】:

我想使用 Rest (Delphi XE7) 使用 Google Api,这样做:

uses
 ...
 {$IF DEFINED(ANDROID)}
    REST.Authenticator.OAuth.WebForm.FMX;
 {$ENDIF}
 {$IF DEFINED(MsWindows)}
    REST.Authenticator.OAuth.WebForm.Win;
 {$ENDIF}

该程序在 Windows 上运行,在该程序更改 AuchCode 以访问令牌并且一切正常之后。

procedure TForm2.OAuth2_GoogleTasks_BrowserTitleChanged(const ATitle:  string;  var DoCloseWebView: boolean);
begin
  if Pos('Success code', ATitle) > 0 then
  begin
    AuthCode := Copy(ATitle, 14, Length(ATitle));
    if (AuthCode <> '') then
    begin
      editactoken.Text:= AuthCode;
      DoCloseWebView := true;
     webform.Release;
    end;
  end;
end;

procedure TForm2.Button59Click(Sender: TObject);
begin
    WebForm:=Tfrm_OAuthWebForm.Create(nil);
    WebForm.OnTitleChanged  := self.OAuth2_GoogleTasks_BrowserTitleChanged;    
    WebForm.ShowWithURL(OAuth2Authenticator1.AuthorizationRequestURI);
end;

但我遇到了一个问题,我无法在 android 上复制验证码:

  1. Form2.OAuth2_GoogleTasks_BrowserTitleChanged 不起作用,因为标题没有改变;
  2. REST.Authenticator.OAuth.WebForm.FMX 中根本没有标题:

            private
    { Private declarations }
    FOnBeforeRedirect: TOAuth2WebFormRedirectEvent;
    FOnAfterRedirect: TOAuth2WebFormRedirectEvent;
    FOnBrowserTitleChanged : TOAuth2WebFormTitleChangedEvent;
    
    
    FLastURL: string;
    public
    

在 REST.Authenticator.OAuth.WebForm.Win 中看起来像这样:

            private
     { Private declarations }
    FOnBeforeRedirect: TOAuth2WebFormRedirectEvent;
    FOnAfterRedirect: TOAuth2WebFormRedirectEvent;
    FOnBrowserTitleChanged : TOAuth2WebFormTitleChangedEvent;


    FLastTitle: string;  // <------- 
    FLastURL: string
  1. fmx 变体中没有属性 webform.lasttitle(在 win 中我可以得到它)
  2. LastUrl 属性在重定向后看起来像这样:“......”但我不明白如何使用它来获取授权码。
  3. 尝试使用 IFMXClipboardService,但无法从 Twebbrowser 获取此文本。
  4. Google 不允许对 lastUrl 地址使用 Http 获取方法来获取响应并解析此代码(错误 405)。
  5. 我看过一些文章说没有办法从webbrowser fmx获取html代码,是这样吗?

任何想法如何在我的程序中获取此代码?

【问题讨论】:

    标签: android delphi authentication oauth google-api


    【解决方案1】:

    Android(安装了 Google Play 服务)为其他 Google API 的身份验证提供了本地解决方案,无需网络浏览器:

    https://developers.google.com/android/guides/http-auth:

    当您希望您的 Android 应用使用用户的 通过 HTTP、GoogleAuthUtil 类和相关 API 的 Google 帐户 为您的用户提供安全一致的选择体验 帐户并为您的应用检索 OAuth 2.0 令牌。

    然后,您可以在基于 HTTP 的通信中使用该令牌 未包含在 Google Play 服务中的 Google API 服务 库,例如 Blogger 或 Translate API。

    我建议在这个方向做一些研究。上面链接的页面还提到了一些(非 HTTP)服务的 GoogleApiClient

    【讨论】:

    • 好吧,我有点难以理解如何将上面链接中的代码从 java 转换为 delphi。没有样品。我也使用这种机制从桌面应用程序访问谷歌驱动器,但无论如何,谢谢你的澄清。
    猜你喜欢
    • 2013-07-23
    • 2015-06-27
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2015-02-20
    • 2012-07-17
    相关资源
    最近更新 更多