【问题标题】:react native and setting up the gmail api反应原生并设置 gmail api
【发布时间】:2016-05-11 18:01:40
【问题描述】:

我在设置 gmail api 时遇到问题。谁能告诉我哪里出错了?

说明:

Run the following command to download the library using git:

git clone --recursive https://github.com/google/google-api-objectivec-client.git

在命令行中,我导航到项目的根目录并运行该命令。

Open Xcode and create a new workspace named "Quickstart".

Using File > Add Files to "Quickstart"..., add the following project to the workspace that you cloned in the previous step:
google-api-objectivec-client/Source/GTLCore.xcodeproj

我没有创建新的工作区,我只是使用现有的工作区。我点击了文件 > 将文件添加到“收件箱”并添加了 GTLCore.xcodeproj

Create a new iOS > Application > Single View Application project named "QuickstartApp", with the organization "com.example". Set the Language to Objective-C, and when saving the project set the Add to and Group fields to "Quickstart".

由于我已经有一个项目,我没有做这一步。

Add the following frameworks and libraries to the project's Build Phases > Link Binary with Libraries section:
GTL.framework (from GTLiOSCore target)

我点击了我的 Inbox 项目,选择了构建阶段并添加了 GTL.framework。

Change the following Build Settings:
Add the flag GTL_BUILT_AS_FRAMEWORK=1 to Preprocessor Macros.

我点击了我的收件箱项目,点击了构建设置,搜索了预处理器宏并添加了那行代码。

Add the file google-api-objectivec-client/Source/OAuth2/Touch/GTMOAuth2ViewTouch.xib to the project's Supporting Files group.

我选择了收件箱测试>支持文件,右键添加文件到收件箱 但是该文件不在它所在的位置

/google-api-objectivec-client/Deps/gtm-oauth2/Source/Touch/GTMOAuth2ViewTouch.xib

将以下文件添加到 QuickstartApp 组:

google-api-objectivec-client/Source/Services/Gmail/Generated/GTLGmail_Sources.m

google-api-objectivec-client/Source/Services/Gmail/Generated/GTLGmail.h

我右键单击我的收件箱文件夹并选择“add files to inbox”,这两个文件都已添加到我的收件箱文件夹中。

此时尝试编译但收到错误'GTLObject.h'file not found

此文件位于第 3 步中添加的 GTLCore.xcodeproj 中。每次我添加其中一个文件时,它都会要求另一个文件。

第 4 步:我创建了自己的文件

gmail.h

#ifndef gmail_h
#define gmail_h
#import <UIKit/UIKit.h>

#import "GTL/GTMOAuth2ViewControllerTouch.h"
#import "GTLGmail.h"

@interface gmail : UIViewController

@property (nonatomic, strong) GTLServiceGmail *service;
@property (nonatomic, strong) UITextView *output;

@end

#endif /* gmail_h */

复制并粘贴到新文件中 gmail.m

#import <Foundation/Foundation.h>

#import "gmail.h"

static NSString *const kKeychainItemName = @"Gmail API";
static NSString *const kClientID = @"this is my key";

@implementation gmail

@synthesize service = _service;
@synthesize output = _output;

// When the view loads, create necessary subviews, and initialize the Gmail API service.
- (void)viewDidLoad {
  [super viewDidLoad];

  // Create a UITextView to display output.
  self.output = [[UITextView alloc] initWithFrame:self.view.bounds];
  self.output.editable = false;
  self.output.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
  self.output.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  [self.view addSubview:self.output];

  // Initialize the Gmail API service & load existing credentials from the keychain if available.
  self.service = [[GTLServiceGmail alloc] init];
  self.service.authorizer =
  [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                        clientID:kClientID
                                                    clientSecret:nil];
}

// When the view appears, ensure that the Gmail API service is authorized, and perform API calls.
- (void)viewDidAppear:(BOOL)animated {
  if (!self.service.authorizer.canAuthorize) {
    // Not yet authorized, request authorization by pushing the login UI onto the UI stack.
    [self presentViewController:[self createAuthController] animated:YES completion:nil];

  } else {
    [self fetchLabels];
  }
}

// Construct a query and get a list of labels from the user's gmail. Display the
// label name in the UITextView
- (void)fetchLabels {
  self.output.text = @"Getting labels...";
  GTLQueryGmail *query = [GTLQueryGmail queryForUsersLabelsList];
  [self.service executeQuery:query
                    delegate:self
           didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}

- (void)displayResultWithTicket:(GTLServiceTicket *)ticket
             finishedWithObject:(GTLGmailListLabelsResponse *)labelsResponse
                          error:(NSError *)error {
  if (error == nil) {
    NSMutableString *labelString = [[NSMutableString alloc] init];
    if (labelsResponse.labels.count > 0) {
      [labelString appendString:@"Labels:\n"];
      for (GTLGmailLabel *label in labelsResponse.labels) {
        [labelString appendFormat:@"%@\n", label.name];
      }
    } else {
      [labelString appendString:@"No labels found."];
    }
    self.output.text = labelString;
  } else {
    [self showAlert:@"Error" message:error.localizedDescription];
  }
}


// Creates the auth controller for authorizing access to Gmail API.
- (GTMOAuth2ViewControllerTouch *)createAuthController {
  GTMOAuth2ViewControllerTouch *authController;
  NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeGmailReadonly, nil];
  authController = [[GTMOAuth2ViewControllerTouch alloc]
                    initWithScope:[scopes componentsJoinedByString:@" "]
                    clientID:kClientID
                    clientSecret:nil
                    keychainItemName:kKeychainItemName
                    delegate:self
                    finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  return authController;
}

// Handle completion of the authorization process, and update the Gmail API
// with the new credentials.
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)authResult
                 error:(NSError *)error {
  if (error != nil) {
    [self showAlert:@"Authentication Error" message:error.localizedDescription];
    self.service.authorizer = nil;
  }
  else {
    self.service.authorizer = authResult;
    [self dismissViewControllerAnimated:YES completion:nil];
  }
}

// Helper for showing an alert
- (void)showAlert:(NSString *)title message:(NSString *)message {
  UIAlertView *alert;
  alert = [[UIAlertView alloc] initWithTitle:title
                                     message:message
                                    delegate:nil
                           cancelButtonTitle:@"OK"
                           otherButtonTitles:nil];
  [alert show];
}

@end

【问题讨论】:

    标签: objective-c react-native gmail-api


    【解决方案1】:

    文档指出“静态库目标还创建了一个包含库标题的文件夹以拖入目标的源”。对应的目录只包含一个头文件GTLDefines.h,它显然没有引用其他必需的,例如GTLObject.h,也没有一个文件夹只包含头文件。我认为有必要手动添加对 其他所需的头文件,例如 Objects/GTLObject.h 到应用程序项目中。

    也试试看这个link。它在这里声明他将头文件从 GTL.proj 导入到他的项目中。其中包括GTLBase64.hGTLBatchQuery.hGTLBatchResult.h 等文件。如果没有标头,他会在导入语句中收到错误 "GTLObject.h file not found."

    【讨论】:

    • 非常感谢,您的建议和链接非常有帮助。我决定用Node Js来处理api。
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2020-12-15
    • 1970-01-01
    • 2017-11-26
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多